public class DBConnectionDAOImpl {
public List<String> getData{
return jdbcTemplate.query("Select abc from table_name ", (ResultSet rs) -> getAllData(rs));
}
public List<String> getAllData(ResultSet rs){
List<String> ls = new ArrayList<>();
try{
while(rs.next()){
ls.add(rs.getString("pk"));
}
return ls;
}catch(Exception e){
}
}
}
为了测试getData方法,我把测试用例写成:
public class DBConnectionDAOImplTest {
@InjectMock
DBConnectionDAOImpl dbConnectionDAOImpl;
@MOck
JdbcTemplate jdbcTemplate;
@Test
public void getDataTest(){
ResultSet rs = Mockito.mock(ResultSet.class);
when(rs.next().thenReturn(true).thenReturn(false));
when(rs.getString("pk")).thenReturn("someVal");
when(dbConnectionDAOImpl.getAllData(rs)).thenReturn(new ArrayList<String>());
when(jdbcTemplate.query("Select abc from table_name ", (ResultSet rs) -> dbConnectionDAOImpl.getAllData(rs))).thenReturn(new ArrayList<String>());
List<String> result = dbConnectionDAOImpl.getData();
assertNotNull(result);
}
}
在运行测试用例时,我收到错误行
when(dbConnectionDAOImpl.getAllData(rs)).thenReturn(new ArrayList<String>());
as&#34; org.mockito.exceptions.misusing.WrongTypeOfReturnValue:&#34;。没有得到我错的地方。
---编辑1-- 更改
后 when(dbConnectionDAOImpl.getAllData(rs)).thenReturn(new ArrayList<String>());
根据评论断言失败。
答案 0 :(得分:1)
在我的评论中提供的链接中,尝试更改:
when(dbConnectionDAOImpl.getAllData(rs)).thenReturn(new ArrayList<String>());
到
doReturn(new ArrayList<String>()).when(dbConnectionDAOImpl).getAllData(rs)