簡體   English   中英

Junit測試用例是否在for循環內

[英]Junit test case for if condition inside for-loop

我的測試用例由於NullPointerException而失敗。 另外,不確定如何在junit中處理或調試此null錯誤。

以下是測試案例代碼:

List<String> groupName = new ArrayList<String>();
when(claimGroupingHistoryRepository.getGroupingKeyName(1l)).thenReturn(groupName);
service.updateClaimGroupingKeyForDealer(dealer.getDealerId(), groupingKeyResource, userId);

由於下面的代碼,在我的測試案例中出現了null pointer錯誤。 如果注釋, if block for-loop內的if block未收到錯誤。 不知道如何在這里測試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;
                  }

              }
          } 
      }

以下是正在得到的測試用例錯誤:

ClaimServiceImplTest.shouldUpdateClaimGroupingKeyForDealerUpdate:6057 » NullPointer

請找到我完整的測試文件

更新時間: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);

    } 

除非我們看到GroupingKeys的定義,否則無法確定這一點,但是獲得NPE的原因是因為字段value不是原始value

為了避免NPE,您可以初始化GroupingKeys ,特別是將value為某個數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM