繁体   English   中英

如何编写使用匿名内部类(如PreparedStatementSetter)的类的JUnit测试?

[英]How to write JUnit test of a class that uses anonymous inner class such as PreparedStatementSetter?

我有以下使用匿名内部类的类( new PreparedStatementSetter() )使用PreparedStatementSetter在查询中设置值并返回对象List

问题是我不确定如何为这种方法编写单元测试。 一旦我的单元测试到达调用getJdbcTemplate().query(......)的行,它就会跳到行尾,并且discAuditLogList值返回null并且单元测试完成,没有任何错误。 当我查看IntelliJ中的代码覆盖率时,所有ps.setString语句都从未执行过。

ProductLogDao.java

//bunch of setter getters here

public List<ProductLogDTO> getProductLogDetail(RequestDTO requestDTO) throws SQLException, DataAccessException {
    logger.info("Inside ProductLogDao.getProductLogDetail" + requestDTO);
    List<ProductLogDTO> productLogList = null;
    final Map<String, String> requestParamMap = requestDTO.getParameters();
    if ("custom".equalsIgnoreCase(requestParamMap.get("recordtype"))) {
        ProductLogList = getProductCustomDetail(senderFeeSql, requestParamMap);
        return ProductLogList;
    }
    return productLogList;
}


public List<ProductLogDTO> getProductCustomDetail(String senderFeeSql,
                                                          final Map<String, String> requestParamMap) throws SQLException {
    @SuppressWarnings("unchecked")
    List<ProductLogDTO> productLogList = getJdbcTemplate().query(senderFeeSql, new PreparedStatementSetter() {
        public void setValues(PreparedStatement ps) throws SQLException {
            /***************************************************************************************************************/
            //FOLLOWING STATMENTS NEVER GET EXECUTED BECAUSE THEY ARE WITHIN ANONYMOUS CLASS (new PreparedStatementSetter())
            /***************************************************************************************************************/
            ps.setString(1, requestParamMap.get("startDfId"));
            ps.setString(2, requestParamMap.get("startDfId"));
            ps.setString(3, requestParamMap.get("chanelId"));
            ps.setString(4, requestParamMap.get("chanelId"));
            ps.setString(5, requestParamMap.get("fromDateTime"));
            ps.setString(6, requestParamMap.get("toDateTime"));
            ps.setString(7, requestParamMap.get("fromDateTime"));
            ps.setString(8, requestParamMap.get("toDateTime"));
        }
    }, new ProductCustomRowMapper());

    if (null != productLogList && (productLogList.size() > 0)) {
        productLogList.get(0).setRecordtype("custom");
        productLogList.get(0).setRecordsFetched(productLogList.get(0).getRecordsFetched());
    }
    return productLogList;
}

ProductLogDaoTest.java

public class ProductLogDaoTest {

    ProductLogDao instance = new ProductLogDao();
    RequestDTO requestDTO = Mockito.mock(RequestDTO.class);
    JdbcTemplate jdbcTemplate = Mockito.mock(JdbcTemplate.class);
    Map<String, String> requestParamMap = new HashMap<>();

    @Before
    public void setUp() throws Exception {
        instance.setJdbcTemplate(jdbcTemplate);
    }

    @Test
    public void getProductLogDetail_W_Custom() throws SQLException, DataAccessException {
        when(requestDTO.getParameters()).thenReturn(requestParamMap);
        requestParamMap.put("recordtype", "custom");
        assertNotNull(instance.getProductLogDetail(requestDTO));
    }

}

永远不要像这样在依赖对象内部创建对象实例: new PreparedStatementSetter()它很难连接此类实例,并且您无法将其更改为模拟对象。 始终执行以下一项操作:

  1. 通过工厂对象方法调用(myBusinessFactoryInstance.createPreparedStatementSetter)创建这些类实例。 如果您更改工厂,则可以创建一个模拟。

  2. 在Spring中,您可以通过构造函数或setter方法注入此依赖项。

如果这样做,您的测试将像是一种魅力。

您已经模拟了JdbcTemplate ,但是还没有模拟您的JdbcTemplate方法。 这就是为什么它返回null的原因。

您可以使用某种测试数据库。 如果是Spring,则可以使用嵌入式的: https : //docs.spring.io/spring/docs/3.0.0.M4/spring-framework-reference/html/ch12s08.html 有关在测试过程中模拟数据库的更多信息,请参考此问题: 如何模拟用于测试的数据库(Java)?

无论哪种方式,您可以检查您是否query()方法调用的Mockito的verify()方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM