gpt4 book ai didi

java - JUnit 的代码覆盖率

转载 作者:行者123 更新时间:2023-11-30 07:37:40 26 4
gpt4 key购买 nike

我正在为一个方法编写一个 JUnit 测试用例,并且我也在尝试获取代码覆盖率。我现有的测试应该涵盖该场景,但由于某种原因,它在 cobertura 报告中仍然显示为红色。

待测试的方法。

public <T extends BaseServiceResponse> T postProcess(T response,
ClientResponse clientResponse) throws EISClientException {

List<Message> messages = response.getMessages();
if(messages==null || messages.size()==0) {
return response;
}

Map<String, Message> messagesMap = populateMessages(response.getMessages());
ConditionOperator condition = getCondition();

switch(condition) {
case OR:
checkORCondition( messagesMap );
break;
case AND:
checkANDCondition( messagesMap );
break;
}
return response;
}

JUnit 测试用例:

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

// Testing OR condition (200 status code)
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));

RetrieveBillingServiceResponse serviceResponse = postProcessFilter.postProcess(response, clientResponse);

assertNotNull(serviceResponse.getMessages());
assertEquals(1, serviceResponse.getMessages().size());
assertTrue(serviceResponse instanceof RetrieveBillingServiceResponse);
assertFalse(serviceResponse.getMessages().isEmpty());
assertEquals("200", serviceResponse.getMessages().get(0).getMessageCode());
assertEquals("Service completed successfully", serviceResponse.getMessages().get(0).getMessageText());
assertEquals(MessageTypeEnum.MESSAGE_TYPE_INFO, serviceResponse.getMessages().get(0).getMessageType());

// Testing OR condition (404 status code)
message.setMessageCode("404");
message.setMessageText("request not found");
message.setMessageType(MessageTypeEnum.MESSAGE_TYPE_INFO);

response.setMessages(Arrays.asList(message));
serviceResponse = postProcessFilter.postProcess(response, clientResponse);

assertNotNull(serviceResponse.getMessages());
assertFalse(serviceResponse.getMessages().isEmpty());
assertEquals(1, serviceResponse.getMessages().size());
assertEquals("404", serviceResponse.getMessages().get(0).getMessageCode());
assertEquals("request not found", serviceResponse.getMessages().get(0).getMessageText());
assertEquals(MessageTypeEnum.MESSAGE_TYPE_INFO, serviceResponse.getMessages().get(0).getMessageType());

/**
* Testing AND condition
* Catching EISClientException
*/
boolean caughtException = false;
try {
postProcessFilter.setCondition(ConditionOperator.AND);

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

serviceResponse = postProcessFilter.postProcess(response, clientResponse);

assertNotNull(serviceResponse.getMessages());
assertEquals("400", postProcessFilter.getMessageCodes());

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

// Null messages
postProcessFilter.setCondition(ConditionOperator.OR);

List<Message> myList = new ArrayList<Message>();
message.setMessageCode(null);

serviceResponse = postProcessFilter.postProcess(response, clientResponse);

assertEquals(0, myList.size());
assertEquals(null, serviceResponse.getMessages().get(0).getMessageCode());
}

我无法覆盖 message == null || 的 if 语句消息大小==0

任何帮助将不胜感激。这对于消息不为空应该很简单,我现有的代码应该已经覆盖它,但我不确定我做错了什么?

谢谢

最佳答案

优素福,

为了进入该代码块,您需要 or 语句中的至少一个条件为真;所以让我们检查这两个条件。

首先,您传递给 postProcess() 方法的响应对象始终具有消息列表,这就是为什么第一个条件 messages == null 没有被遇见了。

其次,您的消息列表不为空(您在测试中添加了消息,并且从未清除过它们),因此第二个条件 messages.size() == 0 有没有满足。

修复:如果清除响应中的所有消息,例如使用 response.setMessages(null),它将进入该 if block ,并且代码覆盖率将认识它。

关于java - JUnit 的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com