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,