[英]Junit test case for if condition inside for-loop
My test cases is getting failed due to NullPointerException
. 我的测试用例由于
NullPointerException
而失败。 Also, am unsure how to handle or debug this null error in junit. 另外,不确定如何在junit中处理或调试此null错误。
Following is the test cases code : 以下是测试案例代码:
List<String> groupName = new ArrayList<String>();
when(claimGroupingHistoryRepository.getGroupingKeyName(1l)).thenReturn(groupName);
service.updateClaimGroupingKeyForDealer(dealer.getDealerId(), groupingKeyResource, userId);
Due to the following piece of code am gettign the null pointer
error in my test cases. 由于下面的代码,在我的测试案例中出现了
null pointer
错误。 If the comment the if block
inside the for-loop
am not getting the error. 如果注释,
if block
for-loop
内的if block
未收到错误。 Not sure how to test the if condition here. 不知道如何在这里测试if条件。
for(GroupingKeys groupingKey : groupingKeyResource.getGroupingKeys()){
if(groupingKey.getValue() != 0) { // no sure how to check this condition using junit
List<String> name = claimGroupingHistoryRepository.getGroupingKeyName(groupingKey.getId());
for(String nameList:name) {
if(!groupingName.equalsIgnoreCase("")) {
groupingName = groupingName.concat(", ").concat(nameList);
} else {
groupingName = nameList;
}
}
}
}
Following is the test case error that am getting : 以下是正在得到的测试用例错误:
ClaimServiceImplTest.shouldUpdateClaimGroupingKeyForDealerUpdate:6057 » NullPointer
Please find my complete test file 请找到我完整的测试文件
Updated: 1* 更新时间:1 *
public void shouldUpdateClaimGroupingKeyForDealerUpdate() throws Exception {
Dealer dealer = make(a(DealerMaker.Dealer));
ClaimGroupingHistory claimGroupingHistory = make(a(ClaimGroupingHistoryMaker.ClaimGroupingHistory));
String userId = "/user/0001";
Status draft = make(a(Draft));
User user = make(a(UserMaker.User, with(UserMaker.id, userId),with(UserMaker.email, "user@example.com"))).toUser();
ArrayList<ClaimGroupingKeyMapping> mapping = new ArrayList<ClaimGroupingKeyMapping>();
ClaimGroupingKeyMapping e = new ClaimGroupingKeyMapping();
GroupingKeys groupingKeys= new GroupingKeys();
groupingKeys.setValue(1l);
groupingKeys.setCode("code");
e.setValue(1l);
e.setGroupingKeys(groupingKeys);
mapping.add(e);
ArrayList<GroupingKeys> grpKey = new ArrayList<GroupingKeys>();
grpKey.add(new GroupingKeys());
GroupingKeyResource groupingKeyResource = new GroupingKeyResource();
groupingKeyResource.setGroupingKeys(grpKey);
ArrayList<ClaimGroupingHistory> historyLog=new ArrayList<ClaimGroupingHistory>();
List<String> groupName=new ArrayList<String>();
when(claimGroupingHistoryRepository.getClaimGroupingHistory(dealer.getDealerId())).thenReturn(historyLog);
when(claimGroupingHistoryRepository.getGroupingKeyName(groupingKeys.getId())).thenReturn(groupName);
service.updateClaimGroupingKeyForDealer(dealer.getDealerId(), groupingKeyResource, userId);
}
There is no way to be sure of this, unless we see the definition of GroupingKeys
, but the reason you are getting NPE is because the field value
is not a primitive. 除非我们看到
GroupingKeys
的定义,否则无法确定这一点,但是获得NPE的原因是因为字段value
不是原始value
。
To avoid NPE, you can initialize GroupingKeys
, specifically value
to some number. 为了避免NPE,您可以初始化
GroupingKeys
,特别是将value
为某个数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.