【发布时间】:2021-03-15 12:39:22
【问题描述】:
无法在测试类中模拟 BeanPropertyRowMapper 数据。尝试了不同的场景。有人可以帮我解决这个问题吗?
它返回空 dtoResposne 对象,但它返回 dto 对象中的数据。当我们将 dto 与 dtoResponse 进行比较时,这里的断言失败了。
下面的代码是相关的 DaoImpl 类
public Dto getViewData(String requestId, List<String> errorLsist) {
String sql = "SELECT * FROM table WHERE id = ?";
return (Dto) getJdbcTemplate().queryForObject(sql, new Object[] { requestId },
new BeanPropertyRowMapper(Dto.class));
}
Below code is related to test class
String sql = "SELECT * FROM dom.deployment WHERE request_id = ?";
List<Dto> data = new ArrayList<>();
Dto dto = new Dto();
dto.setDecisionFlowGuid(guid);
data.add(dto);
Mockito.when(jdbcTemplate.query(
"SELECT * FROM dom.deployment WHERE request_id = " + requestId,
new BeanPropertyRowMapper<>(Dto.class))).thenReturn(data);
Dto dtoResponse = deploymentDaoUtilImpl.getViewData(requestId, errors);
assertEquals(dto, dtoResponse);
Tried with below scenarios as well.
Mockito.when((Dto) jdbcTemplate.queryForObject(sql, new Object[] { requestId },
new BeanPropertyRowMapper<Dto>(Dto.class))).thenReturn(dto);
Mockito.when((Dto) jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Dto.class)))
.thenReturn(dto);
【问题讨论】:
标签: java unit-testing testing mockito