如何模拟junit中另一个类的枚举方法?

时间:2019-05-20 16:31:01

标签: junit4 powermock

我有一个带有执行函数的枚举类HeatMapStrategy,该函数是我从另一个类HeatMapService调用的。当我在junit代码下运行时,它无法创建HeatMapStrategy类的模拟并且无法模拟execute()方法来自HeatMapStrategy类。

@测试      公共无效fetchHeatMapDataOnSuccess()引发异常{

    when(HeatMapStrategy.valueOf(request.getHeatMapStrategy().toUpperCase()).execute(request)).thenReturn(response);
HeatMapResponse testResponse = service.fetchHeatMapData(request);
        assertEquals(response.getJsonTree().get(0).getItem1(), testResponse.getJsonTree().get(0).getItem1());
        assertEquals(response.getJsonTree().get(0).getItem2(), testResponse.getJsonTree().get(0).getItem2());
        assertEquals(response.getJsonTree().get(0).getQ_measure(),
                testResponse.getJsonTree().get(0).getQ_measure());

    } catch (Exception e) {
        e.printStackTrace();
    }

}

公共枚举HeatMapStrategy {

Q_MEASURE("Q_MEASURE") {

    @Override
    public HeatMapResponse execute(RollUpListRequest request) throws IOException,
            InterruptedException, HeatMapException, ClassNotFoundException {
          Long   subCapId =null;
        String jobType=request.getJobType();
        if(jobType.equalsIgnoreCase(HeatMapConstants.CDT)) {
             subCapId =
                Long.valueOf(oracleDao.getSubstitutionCapId(Long.toString(request.getEasId()),
                        Long.toString(request.getIcdtId())));
        }
        else if(jobType.equalsIgnoreCase(HeatMapConstants.CBT)){
        subCapId= Long.valueOf(mariaDao.getSubstitutionCapId(Long.toString(request.getEasId()),
                Long.toString(request.getIcdtId())));
        }
        LOG.info("capId '{}'", subCapId);

        String query = String.format(qMerticQuery, request.getAssortId(), subCapId,
                request.getRollupList(), request.getRollupList());

        LOG.info("Query to be run '{}' ", query);

        return thriftDao.fetchHiveTables(query);



    }

公共类HeatMapService {

public HeatMapResponse fetchHeatMapData(RollUpListRequest request) throws IOException,
        InterruptedException, HeatMapException, ClassNotFoundException {

    HeatMapResponse response = null;
    try {
        response = HeatMapStrategy.valueOf(request.getHeatMapStrategy().toUpperCase())
                .execute(request);
    } catch (HeatMapException ex) {
        LOG.error(HeatMapConstants.HEATMAP_ERROR, ex.getMessage());
        response=HeatMapResponse.builder().error_message(ex.getMessage()).build();
    }
    return response;
}

}

我期望在我调用HeatMapStrategy.valueOf(request.getHeatMapStrategy()。toUpperCase())。execute(request)时 它应该返回我给出的模拟响应。

0 个答案:

没有答案