【发布时间】:2016-02-03 22:10:17
【问题描述】:
我正在为一个方法编写 JUnit 测试用例并增强我的 Cobertura 分支覆盖率,我想捕获异常,但不确定为什么测试没有捕获异常。
待测方法:
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
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);
}
如果消息不为空,它应该捕获异常,但事实并非如此。我做错什么了吗?
谢谢,
【问题讨论】:
-
我在您的测试中没有看到任何对 getCondition() 的调用。
-
我更正了我的代码。我得到了两种类似的方法,并发布了错误的方法。