[JAVA-2590] Added example of @JdbcTest usage

This commit is contained in:
fdpro 2020-09-23 19:31:59 +02:00
parent 20a3070093
commit a2751ea0d5
3 changed files with 35 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package com.baeldung.spring.jdbc.template.testing;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class EmployeeApplication {
}

View File

@ -13,6 +13,10 @@ public class EmployeeDAO {
jdbcTemplate = new JdbcTemplate(dataSource);
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int getCountOfEmployees() {
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM EMPLOYEE", Integer.class);
}

View File

@ -0,0 +1,24 @@
package com.baeldung.spring.jdbc.template.testing;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.jdbc.Sql;
import static org.junit.jupiter.api.Assertions.assertEquals;
@JdbcTest
@Sql({"schema.sql", "test-data.sql"})
class EmployeeDAOIntegrationTest {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
void whenInjectInMemoryDataSource_thenReturnCorrectEmployeeCount() {
EmployeeDAO employeeDAO = new EmployeeDAO();
employeeDAO.setJdbcTemplate(jdbcTemplate);
assertEquals(4, employeeDAO.getCountOfEmployees());
}
}