我似乎无法模拟 OfferRowMapper 类,因为它在方法中创建了一个新实例。我无法制作 OfferRowMapper 我正在尝试在这里对该方法进行单元测试:
@Override
public List<OfferDetailDTO> getOfferDetails(String programType, Integer pageNo, Integer pageSize) {
OfferRowMapper rowMapper = new OfferRowMapper();
try {
jdbcTemplate.query(query, rowMapper, new Object[]{programType, pageNo, pageSize});
log.info(Logger.EVENT_UNSPECIFIED, String.format(Constants.INFO_109, programType, rowMapper.getData().size()));
} catch (Exception e) {
log.error(Logger.EVENT_FAILURE, String.format(ErrorCodeConstants.ERROR_107, programType, e.getMessage()));
throw new GenericException(e.getMessage());
}
return rowMapper.getData();
}
到目前为止这是我的单元测试,它失败了,因为 rowMapper.getData() 没有返回任何东西,因为它无法被模拟。
@Test
void testGetOfferDetails_thenSuccess() {
// mock
ReflectionTestUtils.setField(JDBCDataAccessLayer, "query", "select query");
OfferRowMapper offerRowMapper = Mockito.mock(OfferRowMapper.class);
OfferDetailDTO OfferDetailDTO = new OfferDetailDTO();
OfferDetailDTO.setOmsOfferId(2506763L);
OfferDetailDTO.setPriceText("$5.20 Each");
OfferDetailDTO.setProgramTypeCd("W");
OfferDetailDTO.setHeadlineTxt("Gefilte Fish");
List<OfferDetailDTO> OfferDetailDTOList = new ArrayList<>();
OfferDetailDTOList.add(OfferDetailDTO);
Mockito.when(JDBCDataAccessLayer.createOfferRowMapper()).thenReturn(offerRowMapper);
Mockito.doNothing().when(jdbcTemplate).query(Mockito.anyString(), Mockito.any(OfferRowMapper.class), Mockito.any(Object.class));
// call and assert
List<OfferDetailDTO> OfferDetailDTOS = JDBCDataAccessLayer.getOfferDetails("W", 0, 30);
Assertions.assertNotNull(OfferDetailDTOS);
Assertions.assertEquals(1, OfferDetailDTOS.size());
Assertions.assertEquals("W", OfferDetailDTOS.get(0).getProgramTypeCd());
Assertions.assertEquals("Gefilte Fish", OfferDetailDTOS.get(0).getHeadlineTxt());
Assertions.assertEquals("$5.20 Each", OfferDetailDTOS.get(0).getPriceText());
Assertions.assertEquals(2506763L, OfferDetailDTOS.get(0).getOmsOfferId());
}
我不能为 rowmapper 使用构造函数注入,因为它实现了 RowCallbackHandler
public class OfferRowMapper implements RowCallbackHandler {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(Constants.DATE_TIME_FORMAT);
private List<OfferDetailDTO> rsMappedRows = new ArrayList<>();
@Override
public void processRow(ResultSet rs) throws SQLException {
OfferDetailDTO OfferDetailDTO = new OfferDetailDTO();
OfferDetailDTO.setOmsOfferId(rs.getLong("oms_offer_id"));
OfferDetailDTO.setProgramTypeCd(rs.getString("program_type_cd"));
OfferDetailDTO.setDisclaimer(rs.getString("disclaimer"));
OfferDetailDTO.setPriceText(rs.getString("price_txt"));
OfferDetailDTO.setHeadlineTxt(rs.getString("headline_txt"));
OfferDetailDTO.setHeadline2Txt(rs.getString("headline2_txt"));
OfferDetailDTO.setLand(rs.getString("land"));
OfferDetailDTO.setSpace(rs.getString("space"));
OfferDetailDTO.setSlot(rs.getString("slot"));
rsMappedRows.add(OfferDetailDTO);
}
public List<OfferDetailDTO> getData() {
return rsMappedRows;
}
}
有人可以指导我如何测试这个场景吗?
你永远无法嘲笑
OfferRowMapper
如果您保留代码,则在 getOfferDetails
方法中使用:
OfferRowMapper rowMapper = new OfferRowMapper();
在getOfferDetails
方法中。
为了嘲笑
OfferRowMapper
,
您必须执行以下操作之一:
getOfferDetails
方法。
有了这个技术,
你会在你的单元测试中创建一个OfferRowMapper
的模拟并将它传递进去。getOfferDetails
方法中使用的类字段。
有了这个技术,
您将在单元测试中创建 OfferRowMapper
的模拟并将类变量设置为此模拟。OfferRowMapper
。
有了这个技术,
你会在你的单元测试中存根这个方法并让它返回一个模拟对象。