[英]Code Coverage with JUnit
我正在為一個方法編寫一個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());
}
我無法涵蓋消息的if語句== null || message.size == 0
任何幫助將不勝感激。 對於消息不為null的情況,這應該很簡單,而我現有的代碼應該已經覆蓋了它,但是我不確定自己做錯了什么?
謝謝
優素福,
為了進入該代碼塊,您至少需要or語句中的其中一個條件為true; 因此,讓我們檢查兩個條件。
首先,傳遞給postProcess()
方法的響應對象始終具有消息列表,這就是為什么不滿足第一個條件messages == null
原因。
其次,您的消息列表不為空(您已經在測試中添加了消息,但從未清除過它們),因此第二個條件messages.size() == 0
不滿足。
修復:如果您從響應中清除所有消息,例如使用response.setMessages(null)
,則它將進入該if
塊,並且代碼覆蓋范圍將識別出它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.