mockMvc.perform方法抛出json不能为null或为空的异常

时间:2019-03-03 11:24:53

标签: java junit mockmvc

正在为我的控制器类编写Junit测试用例,同时使用mockMvc.perform()方法并获得json can not be null or empty excption

我不太确定问题出在哪里。

请在下面找到我的代码。

 @Test
  public void shouldGetClaimGroupingHistoryForDealers() throws Exception {
    String dealerId = "1000" ;
    GroupingKeys groupingKeys = make(a(GroupingKeysResourceMaker.GroupingKeys));
    List<ClaimGroupingHistory> groupingKeysList= new ArrayList<ClaimGroupingHistory>();
    //groupingKeysList.add(groupingKeys);
    ClaimGroupingHistoryResource resource = new ClaimGroupingHistoryResource(groupingKeysList);

    System.out.println(" shouldGetClaimGroupingHistoryForDealers Resource --->"+resource.getClaimGroupingHistoryList().size()); // here size is giving 0

    when(service.getClaimGroupingHistory(dealerId)).thenReturn(null);
    String uri = format("/claims/claim-grouping-history/%s",dealerId);

    //mockMvc.perform(get(uri)).andExpect(status().isOk()).andExpect(jsonPath("$.resource").value(hasSize(1)));
    //mockMvc.perform(get(uri)).andExpect(status().isOk()).andExpect(jsonPath("$.resource").doesNotExist());
    mockMvc.perform(get(uri).andExpect(status().isOk()));
    verify(service, times(1)).getClaimGroupingHistory(dealerId);
  }

请在下面找到错误

Tests run: 60, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.669 sec <<< FAILURE! - in com.cat.pscs.controller.claim.ClaimControllerTest
shouldGetClaimGroupingHistoryForDealers(com.cat.pscs.controller.claim.ClaimControllerTest)  Time elapsed: 0.048 sec  <<< ERROR!
java.lang.IllegalArgumentException: json can not be null or empty
        at com.jayway.jsonpath.internal.Utils.notEmpty(Utils.java:164)
        at com.jayway.jsonpath.JsonPath.read(JsonPath.java:290)
        at com.jayway.jsonpath.JsonPath.read(JsonPath.java:277)
        at org.springframework.test.util.JsonPathExpectationsHelper.evaluateJsonPath(JsonPathExpectationsHelper.java:71)
        at org.springframework.test.util.JsonPathExpectationsHelper.assertValue(JsonPathExpectationsHelper.java:64)
        at org.springframework.test.web.servlet.result.JsonPathResultMatchers$1.match(JsonPathResultMatchers.java:55)
        at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:149)
        at com.cat.pscs.controller.claim.ClaimControllerTest.shouldGetClaimGroupingHistoryForDealers(ClaimControllerTest.java:1292)

0 个答案:

没有答案