简体   繁体   中英

Testing methods using JUnit

I am new to JUnit and I have to test a method using JUnit api. One method internall calls another. My test case goes inside the method but while catchign the exception it fails.

Method under test is

public void checkANDCondition( Map<String, Message> messagesMap ) throws EISClientException
{
    List<String> codes = getMessageCodes();
    if(isAllReturnedMessagesContainCodes(codes, messagesMap))
    {
        StringBuffer buff = new StringBuffer("All of the specified message codes matched returned errors.");
        for(String code: codes )
        {
            Message message = messagesMap.get(code);
            buff.append(message.getMessageCode() + ": " + message.getMessageType() + ": " + message.getMessageText() + " ");
        }
        throw new EISClientException(buff.toString());
    }
}

public boolean isAllReturnedMessagesContainCodes(List<String> codes, Map<String, Message> messagesMap)
{
    if(codes!=null)
    {
        for(String code: codes)
        {
            if(!messagesMap.containsKey(code))
            {
                return false;
            }
        }
    }
    return true;
}

What I have done so far is

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

    MessageToExceptionPostProcessFilter postProcessFilter = new MessageToExceptionPostProcessFilter();
    RetrieveBillingServiceResponse serviceResponse = new RetrieveBillingServiceResponse();caughtException = false;
    try {
        postProcessFilter.setCondition(ConditionOperator.AND);

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

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

        serviceResponse.setMessages(Arrays.asList(message));
        postProcessFilter.setMessageCodes(messagesCodes);

        serviceResponse = postProcessFilter.postProcess(serviceResponse, clientResponse);

        assertNotNull(serviceResponse.getMessages());

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

}

How can I make it pass?

Thanks

@Test(expected = EISCLientException.class)
public void testPostProcess() throws Exception {
...
serviceResponse.getMessages();
fail("Shouldn't reach this point");
}

That way you don't need to catch, with expected if it does not get throw a EISClientException it will fail.

edit: There are two times I can think of where you wouldn't want to use this.

1) You are mocking exceptions that are thrown mock(exception.class); this i believe then throws some Mockito excpetion and it will not match the expected exception.

2) You are wrapping caught exceptions in your code, and throwing a generic exception. Example of code:

try {
} catch (FileParseException e){

throw new (ProjectFailingException(e, "file is bad");
}

if you have multiple catches and are wrapping them as ProjectFailingExceptions then you may want to catch in the test like this...

@Test ( expected = FileParseException.class)
public void testProcess() {
try {
...
} catch (ProjectFailingException e){
throw e.getCause();
}

Then the proper exception is thrown and you can make sure that process isn't throwing an exception from aa different catch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM