当前位置: 代码迷 >> java >> Junit EasyMock,resultSet.getBigDecimal(“”)返回空值
  详细解决方案

Junit EasyMock,resultSet.getBigDecimal(“”)返回空值

热度:73   发布时间:2023-07-25 19:41:35.0

我已经使用EasyMock和PowerMock构建了一个模拟对象。 当我为getBigDecimal(“”)方法设置期望行为时,它返回null

@Test
public void getProductPriceContractInfo_v1_1Test() throws Exception{

    GetSPDProductPricePersistor productPricePersistor = new GetSPDProductPricePersistor();
    ProductPriceAndContractRequest_v1_1 productPriceAndContractRequest = new ProductPriceAndContractRequest_v1_1();
   /* set all request parameters in productPriceAndContractRequest --- */

    PowerMock.mockStatic(DataSourceManager.class); 
    DataSourceManager dBAccessor = PowerMock.createMock(DataSourceManager.class);
    EasyMock.expect(DataSourceManager.getInstance()).andReturn(dBAccessor).anyTimes();
    EasyMock.expect(DataSourceManager.freeConnection((Connection)EasyMock.anyObject(), EasyMock.anyObject(),
            EasyMock.anyObject())).andReturn(true);

    Connection connection = EasyMock.createMock(Connection.class);  
    PreparedStatement ps = EasyMock.createMock(PreparedStatement.class);    

    EasyMock.expect(dBAccessor.getConnection(EasyMock.anyString())).andReturn(connection).anyTimes();       

    connection.close();
    connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
    EasyMock.expect(connection.prepareStatement((String)EasyMock.anyObject())).andReturn(ps);

    ResultSet rs = EasyMock.createMock(ResultSet.class);
    EasyMock.expect(rs.next()).andReturn(true);
    EasyMock.expect(rs.next()).andReturn(false);
    EasyMock.expect(rs.getString("")).andReturn("Y").times(4);
    EasyMock.expect(rs.getBigDecimal("")).andReturn((BigDecimal) EasyMock.anyObject()).anyTimes();
    EasyMock.expect(rs.getBigDecimal("")).andReturn((BigDecimal) EasyMock.anyObject()).anyTimes();
    EasyMock.expect(rs.getBigDecimal((""))).andReturn((BigDecimal) EasyMock.anyObject()).anyTimes();
    EasyMock.expect(rs.getDate("")).andReturn((java.sql.Date) EasyMock.anyObject()).anyTimes();
    EasyMock.expect(rs.getBigDecimal("")).andReturn(BigDecimal.TEN).anyTimes();
    EasyMock.expect(rs.getBigDecimal("")).andReturn((BigDecimal) EasyMock.anyObject()).anyTimes();
    EasyMock.expect(rs.getBigDecimal("")).andReturn(BigDecimal.valueOf(987654L)).anyTimes();
    EasyMock.expect(rs.getInt("")).andReturn(5).anyTimes();
    EasyMock.expect(rs.getString("")).andReturn("").anyTimes();
    EasyMock.expect(rs.getString("")).andReturn("").anyTimes();
    EasyMock.expect(rs.getString("")).andReturn("").anyTimes();

    EasyMock.expect(ps.executeQuery()).andReturn(rs);   // returning ResultSet          
    EasyMock.expect(ps.executeQuery()).andReturn(rs);


    ps.setString(EasyMock.anyInt(),String.valueOf(EasyMock.anyInt()));  
    ps.setString(EasyMock.anyInt(),String.valueOf(EasyMock.anyInt()));
    ps.setString(EasyMock.anyInt(),String.valueOf(EasyMock.anyInt()));  
    ps.setString(EasyMock.anyInt(),String.valueOf(EasyMock.anyInt()));
    ps.setString(EasyMock.anyInt(),String.valueOf(EasyMock.anyInt()));
    ps.setString(EasyMock.anyInt(),String.valueOf(EasyMock.anyInt()));  
    ps.setString(EasyMock.anyInt(),String.valueOf(EasyMock.anyInt()));
    ps.close();

    PowerMock.expectLastCall().anyTimes();  

    PowerMock.replay(DataSourceManager.class);
    PowerMock.replay(dBAccessor);
    EasyMock.replay(connection);
    EasyMock.replay(ps);
    EasyMock.replay(rs);


    ProductPriceAndContractResponse_v1_1 response = (ProductPriceAndContractResponse_v1_1)productPricePersistor.getProductPriceContractInfo_v1_1(productPriceAndContractRequest);
    assertNotNull(response);
}

当我调试它时

   public BaseResponse getProductPriceContractInfo_v1_1 (parameters) {
        Connection connection = null;

        PreparedStatement ps = null;

        ResultSet rs = null;

        try {
            connection = getConnection(ProductPricingInfoConstants.SPD_ODS_DATASOURCE_JNDI); // class method call
            ps = connection.prepareStatement(sqlQuery.toString());
            ps.setString(1, custNo);
            ps.setString(2, divNo);
            //made changes for 14.5 CI Release--START
             ps.setString(3, custNo);
             ps.setString(4, divNo); //PCRD-15.2 Specialty Online Requirements-Batch I

             int index = 5; //PCRD-15.2 Specialty Online Requirements-Batch I

            //made changes for 14.5 CI Release--END
            for (String cin : itmLst) {
                ps.setString(index++, cin);
            }

            rs = ps.executeQuery();

            if (null != rs) {
                ContractInfo contractInfo;
                ProductPriceInfo productPriceInfo;

                while (rs.next()) {
                    productPriceInfo = new ProductPriceInfo();

                    String str = rs.getString("")
                    productPriceInfo.setAltProductContract();

                    int num = rs.getInt("");

                    BigDecimal cntrNumber = rs.getBigDecimal("");
                    productPriceInfo.setContractNumber(cntrNumber.toString());
}

我在numstr变量中得到了期望值,但在cntrNumber变量中得到了null值。

只能通过以下方式使用对getBigDecimal一种期望:

EasyMock.expect(rs.getBigDecimal("")).andReturn((BigDecimal) EasyMock.anyObject()).anyTimes();

anyTimes方法表示可以anyTimes次调用此方法,因此不会使用对getBigDecimal的其他任何期望。

测试完成后不会调用EasyMock.verify 如果这样做,则测试将失败,因为未达到期望值,从而突出了此问题。

getBigDecimal返回null的原因是因为EasyMock.anyObject()返回null EasyMock.anyObject()方法不打算用作返回值,正如在文档中指定的那样,它创建一个匹配器 ,该匹配器将匹配传递给方法的任何Object参数。

顺便说一句,这样的测试几乎没有用,它们只是测试代码调用被测试方法中指定的方法,您可以确定Java会这样做。

您应该使用作为独立于单元测试的套件运行的验收测试来测试实际的数据库集成。

  相关解决方案