简体   繁体   中英

Junit test case for if condition inside for-loop

My test cases is getting failed due to NullPointerException . Also, am unsure how to handle or debug this null error in junit.

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. If the comment the if block inside the for-loop am not getting the error. Not sure how to test the if condition here.

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*

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.

To avoid NPE, you can initialize GroupingKeys , specifically value to some number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM