如何在TestNG中使用Mockito模拟jdbc连接和结果集
2022-09-03 00:38:17
我必须编写一些单元测试,但我有模拟和jdbc的问题。ResultSet
Connection
我有这个方法:
@Test
public void test3() throws SQLException, IOException {
Connection jdbcConnection = Mockito.mock(Connection.class);
ResultSet resultSet = Mockito.mock(ResultSet.class);
Mockito.when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false);
Mockito.when(resultSet.getString(1)).thenReturn("table_r3").thenReturn("table_r1").thenReturn("table_r2");
Mockito.when(jdbcConnection
.createStatement()
.executeQuery("SELECT name FROM tables"))
.thenReturn(resultSet);
//when
List<String> nameOfTablesList = null;
try {
nameOfTablesList = Helper.getTablesName(jdbcConnection);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//then
Assert.assertEquals(nameOfTablesList.size(), 3);
}
错误显示在行中,听起来像这样:executeQuery("SELECT name FROM tables")
java.lang.NullPointerException HelperTest.test3(HelperTest.java:71)
任何想法出了什么问题?