Merge pull request #10080 from dupirefr/team/JAVA-2590-jdbctest_annotation
[JAVA-2590] Added example of @JdbcTest usage
This commit is contained in:
commit
952e7d3a0d
@ -0,0 +1,7 @@
|
||||
package com.baeldung.spring.jdbc.template.testing;
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class EmployeeApplication {
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user