简体   繁体   English

使用 JUnit 的测试方法

[英]Testing methods using JUnit

I am new to JUnit and I have to test a method using JUnit api.我是 JUnit 的新手,我必须使用 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.这样你就不需要捕获,如果它没有抛出 EISClientException,它就会失败。

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); 1) 你在mock(exception.class);抛出的异常mock(exception.class); this i believe then throws some Mockito excpetion and it will not match the expected exception.我相信这会引发一些 Mockito excpetion 并且它不会匹配预期的异常。

2) You are wrapping caught exceptions in your code, and throwing a generic exception. 2)您在代码中包装捕获的异常,并抛出通用异常。 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...如果您有多个捕获并将它们包装为 ProjectFailingExceptions,那么您可能希望像这样在测试中捕获...

@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.然后抛出正确的异常,您可以确保该进程不会从不同的捕获中抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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