Reputation:
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,
Upvotes: 0
Views: 309
Reputation: 491
There are a couple of things that are not OK here:
Use correct annotation for this kind of test
@Test(expected = EISClientException.class)
It is a bad practice to have multiple asserts in a unit test. See 1 - that should fix it.
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.
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.
Upvotes: 0
Reputation: 353
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
Upvotes: 0
Reputation: 2621
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.
Upvotes: 1
Reputation: 81
Upvotes: 0