What i do wrong? Always getting NPE after test. Test logs:
14:35:36,684 INFO EmbeddedDatabaseFactory:185 - Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa' 14:35:36,690 DEBUG DataSourceUtils:110 - Fetching JDBC Connection from DataSource 14:35:36,691 DEBUG SimpleDriverDataSource:138 - Creating new JDBC Driver Connection to [jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false] 14:35:36,779 INFO ScriptUtils:444 - Executing SQL script from class path resource [db/create-db.sql] 14:35:36,794 DEBUG ScriptUtils:476 - 0 returned as update count for SQL: CREATE TABLE DEPARTMENT ( id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL, DEPARTMENTNAME CHAR NOT NULL ) 14:35:36,796 DEBUG ScriptUtils:476 - 0 returned as update count for SQL: CREATE UNIQUE INDEX "DEPARTMENT_id_uindex" ON DEPARTMENT (id) 14:35:36,797 DEBUG ScriptUtils:476 - 0 returned as update count for SQL: CREATE UNIQUE INDEX "DEPARTMENT_DEPARTMENTNAME_uindex" ON DEPARTMENT (DEPARTMENTNAME) 14:35:36,797 INFO ScriptUtils:510 - Executed SQL script from class path resource [db/create-db.sql] in 18 ms. 14:35:36,797 INFO ScriptUtils:444 - Executing SQL script from class path resource [db/insert-data.sql] 14:35:36,800 DEBUG ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO DEPARTMENT VALUES (1, 'Java') 14:35:36,801 DEBUG ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO DEPARTMENT VALUES (2, 'dotNOT') 14:35:36,801 DEBUG ScriptUtils:476 - 1 returned as update count for SQL: INSERT INTO DEPARTMENT VALUES (3, 'HR') 14:35:36,801 INFO ScriptUtils:510 - Executed SQL script from class path resource [db/insert-data.sql] in 4 ms. 14:35:36,801 DEBUG DataSourceUtils:327 - Returning JDBC Connection to DataSource 14:35:36,834 INFO EmbeddedDatabaseFactory:217 - Shutting down embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false' 14:35:36,834 DEBUG SimpleDriverDataSource:138 - Creating new JDBC Driver Connection to [jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false]
java.lang.NullPointerException at com.dao.access.JdbcDepartmentDao.findAll(JdbcDepartmentDao.java:54) at com.DepartmentControllerTest.testDao(DepartmentControllerTest.java:32) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code -1
DepartmentControllerTest class code:
private EmbeddedDatabase db;
@Before
public void setUp() {
db = new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("db/create-db.sql")
.addScript("db/insert-data.sql")
.build();
}
@Test
public void testDao(){
JdbcDepartmentDao departmentDao = new JdbcDepartmentDao();
List<Department> departments = departmentDao.findAll();
assertEquals(3, departments.size());
}
@After
public void tearDown() {
db.shutdown();
}
DepartmentController class method:
@RequestMapping(method = RequestMethod.GET)
public List<Department> getAllDepartments(){
return departmentDao.findAll();
}
Dao method:
@Override
@Autowired
public void setDataSource(DataSource dataSource) {
jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
simpleJdbcInsert = new SimpleJdbcInsert(dataSource)
.withTableName("department")
.usingGeneratedKeyColumns("id");
}
@Override
public List<Department> findAll() {
String sql = "select id, departmentName from department";
return jdbcTemplate.query(sql, (rs, rowNum) -> {
Department department = new Department();
department.setId(rs.getLong("id"));
department.setDepartmentName(rs.getString("departmentName"));
return department;
});
}
And DataSource code:
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder
.setType(EmbeddedDatabaseType.H2)
.addScript("db/create-db.sql")
.addScript("db/insert-data.sql")
.build();
}
Aucun commentaire:
Enregistrer un commentaire