如何解决当始终返回null时,junit5 Mockito jdbctemplate
我有一个jdbcTemplate调用返回了一些对象。单元测试始终返回null。
public Person retrievePerson(String lastName,long id) {
try {
return jdbcTemplate
.queryForObject(GET_PERSON_DETAIL,new Object[]{lastName,id},(rs,rowId) -> new Person(rs.getString("ssn"),rs.getString("phoneNo")));
} catch (DataAccessException dataAccessException) {
throw new PersonNotFoundException(
"no database records exists for the given lastName " + lastName
+ " and id " + id + " the actual error message is "
+ dataAccessException.getMessage());
}
}
对应的单元测试
@Test
void testRetrievePerson() {
// Setup
final Person expectedResult = new person(
"111111111","1234567890");
// Configure JdbcTemplate.queryForObject(...).
final Person person = new Person(
"111111111","1234567890");
// when(mockJdbcTemplate.queryForObject(eq("sql"),any(Object[].class),//any(RowMapper.class))).thenReturn(person);
doReturn(person).when(
mockJdbcTemplate).queryForObject(eq("sql"),any(RowMapper.class));
// Run the test
final Person result = personClassUnderTest
.retrievePerson("lastName",0L);
// Verify the results
assertThat(result).isEqualTo(expectedResult);
}
我同时尝试了method和doReturn。我总是得到的作为空。另外,另一个测试涉及DataAccessException java.lang.InstantiationError: org.springframework.dao.DataAccessException
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。