JUnit的代码覆盖率

时间:2016-02-02 20:22:28

标签: java junit cobertura

我正在为一个方法编写一个JUnit测试用例,我也试图获得代码覆盖率。我现有的测试应该涵盖场景,但由于某种原因,它仍然在cobertura报告中显示为红色。

要测试的方法。

public <T extends BaseServiceResponse> T postProcess(T response,
        ClientResponse clientResponse) throws EISClientException {  

    List<Message> messages = response.getMessages();
    if(messages==null || messages.size()==0) {
        return response;
    }

    Map<String, Message> messagesMap = populateMessages(response.getMessages());
    ConditionOperator condition = getCondition();

    switch(condition) {
        case OR:
            checkORCondition( messagesMap );
            break;
        case AND:
            checkANDCondition( messagesMap );
            break;
    }       
    return response;
}   

JUnit测试用例:

@Test
public void testPostProcess() throws Exception {
    clientResponse = mock(ClientResponse.class);
    RetrieveBillingServiceResponse response = new RetrieveBillingServiceResponse();

    // Testing OR condition (200 status code)
    MessageToExceptionPostProcessFilter postProcessFilter = new MessageToExceptionPostProcessFilter();
    postProcessFilter.setCondition(ConditionOperator.OR);

    Message message = new Message();
    message.setMessageCode("200");
    message.setMessageType(MessageTypeEnum.MESSAGE_TYPE_INFO);
    message.setMessageText("Service completed successfully");

    response.setMessages(Arrays.asList(message));

    RetrieveBillingServiceResponse serviceResponse = postProcessFilter.postProcess(response, clientResponse);

    assertNotNull(serviceResponse.getMessages());
    assertEquals(1, serviceResponse.getMessages().size());
    assertTrue(serviceResponse instanceof RetrieveBillingServiceResponse);
    assertFalse(serviceResponse.getMessages().isEmpty());
    assertEquals("200", serviceResponse.getMessages().get(0).getMessageCode());
    assertEquals("Service completed successfully", serviceResponse.getMessages().get(0).getMessageText());
    assertEquals(MessageTypeEnum.MESSAGE_TYPE_INFO, serviceResponse.getMessages().get(0).getMessageType());     

    // Testing OR condition (404 status code)
    message.setMessageCode("404");
    message.setMessageText("request not found");
    message.setMessageType(MessageTypeEnum.MESSAGE_TYPE_INFO);

    response.setMessages(Arrays.asList(message));
    serviceResponse = postProcessFilter.postProcess(response, clientResponse);

    assertNotNull(serviceResponse.getMessages());
    assertFalse(serviceResponse.getMessages().isEmpty());
    assertEquals(1, serviceResponse.getMessages().size());
    assertEquals("404", serviceResponse.getMessages().get(0).getMessageCode());
    assertEquals("request not found", serviceResponse.getMessages().get(0).getMessageText());
    assertEquals(MessageTypeEnum.MESSAGE_TYPE_INFO, serviceResponse.getMessages().get(0).getMessageType());     

    /**
     * Testing AND condition
     * Catching EISClientException
     */
    boolean caughtException = false;
    try {
        postProcessFilter.setCondition(ConditionOperator.AND);

        List<String> myMessages = new ArrayList<String>();
        myMessages.add("200");
        myMessages.add("400");

        serviceResponse = postProcessFilter.postProcess(response, clientResponse);

        assertNotNull(serviceResponse.getMessages());
        assertEquals("400", postProcessFilter.getMessageCodes());

    } catch (EISClientException ex) {
        caughtException = true;
        assertEquals("All of the specified message codes matched returned errors.", ex.getMessage());
    }
    assertTrue(caughtException);

    // Null messages
    postProcessFilter.setCondition(ConditionOperator.OR);

    List<Message> myList = new ArrayList<Message>();
    message.setMessageCode(null);

    serviceResponse = postProcessFilter.postProcess(response, clientResponse);

    assertEquals(0, myList.size());
    assertEquals(null, serviceResponse.getMessages().get(0).getMessageCode());
}   

我无法覆盖message == null ||的if语句message.size == 0

任何帮助都将受到高度赞赏。这应该很简单,消息不能为空,我现有的代码应该覆盖它,但我不确定我做错了什么?

由于

1 个答案:

答案 0 :(得分:1)

优素福,

为了进入该代码块,您需要在or语句中至少有一个条件为真;所以让我们检查两个条件。

首先,传递给postProcess()方法的响应对象始终有一个消息列表,这就是为什么没有满足第一个条件messages == null的原因。

其次,您的邮件列表不为空(您已在测试中添加了邮件,并且从未清除过它们),因此未满足第二个条件messages.size() == 0

修复:如果您清除了来自回复的所有邮件,例如response.setMessages(null),那么它将进入该if块,代码覆盖率将识别它。