0

I am writing a JUnit test case for a method and to enhance my Cobertura branch coverage I want to catch the exception but not sure why the test doesn't catch the exception.

Method to be tested:

 public void getCondition( Map<String, Message> messagesMap ) throws EISClientException
{
    Message message = containsAMessageCode(getMessageCodes(), messagesMap);
    if(message!=null)
    {
        throw new EISClientException("One of the specified message code matched returned errors." + 
                message.getMessageCode() + ": " + message.getMessageType() + ": " + message.getMessageText());

    }
}

JUnit test:

@Test
public void testgetCondition() throws Exception {
    boolean caughtException = false;
    try {
        clientResponse = mock(ClientResponse.class);
        RetrieveBillingServiceResponse response = new RetrieveBillingServiceResponse();

        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));

        Map<String, Message> map = new HashMap<String, Message>();
        map.put("test", message);

        RetrieveBillingServiceResponse serviceResponse = postProcessFilter.getCondition(map);

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

If the message is not null it should catch the exception but it is not. Am I doing anything wrong?

Thanks,

2
  • I don't see any call to getCondition() in your test. Commented Feb 3, 2016 at 22:12
  • I corrected my code. I got two similar methods and posted the wrong one. Commented Feb 3, 2016 at 22:18

4 Answers 4

1

Another way of testing the expected exception was thrown is to have a fail() after the line of code under test that causes the exception.

If the exception is thrown you jump to your catch and the fail() is never called, if no exception is thrown the fail() executes following the line that should have thrown an exception and your test fails.

Also, look at your current assertEquals() - the String you are comparing with is not the same as the String you are building when you create the Exception.

Sign up to request clarification or add additional context in comments.

Comments

0
  1. @yole is right. you haven't called the method under test.
  2. a better way to achieve the same thing would be to use the @Test(expected = .class) annotation - of course - you won't be able to check for the message. But in your case the method under test is throwing just one exception - So as long as you are testing that it throws that exception - it should be fine. Needless to say cobertura - will be happy ! here's a tutorial: http://www.mkyong.com/unittest/junit-4-tutorial-2-expected-exception-test/

2 Comments

You should never use @ExpectException in my professional opinion, you should catch the exception and check the cause and message or as you expect. Just my professional opinion. :)
@Gavin ExpectedException have an expectMessage function.
0

Are you sure that your test somewhere call getCondition() method? I don't see it from your code. But if you sure that getCondition() was called, try to use !message.isEmpty() instead of message!=null

2 Comments

I call it in RetrieveBillingServiceResponse serviceResponse = postProcessFilter.getCondition(map);
Oh, yes, I see now. What about using !message.isEmpty() instead of message!=null in getCondition() method? Didn't help? Or your Message object haven't this method (isEmpty)? If have, I think the problem may be here, because checking for null and checking for empty is not the same.
0

There are a couple of things that are not OK here:

  1. Use correct annotation for this kind of test

    @Test(expected = EISClientException.class)

  2. It is a bad practice to have multiple asserts in a unit test. See 1 - that should fix it.

  3. What about containsAMessageCode(getMessageCodes(), messagesMap); . Are you sure this is not returning null ? If you really want to unit test getCondition method you should short-circuit that call.

  4. The assertEquals in catch (EISClientException ex) { ... } doesn't seem OK. You are throwing an exception with a more complicated message, it would never be true as your code looks like now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.