Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing JSON路径上没有值_Unit Testing_Spring Mvc_Mockito_Jsonpath_Mockmvc - Fatal编程技术网

Unit testing JSON路径上没有值

Unit testing JSON路径上没有值,unit-testing,spring-mvc,mockito,jsonpath,mockmvc,Unit Testing,Spring Mvc,Mockito,Jsonpath,Mockmvc,如何为下面结合了字符串和数组的JSON编写mockMVC测试 { "id":1, "firstName":"NPA", "lastName":"TAS", "mobile":"123454321", "email":"ABCD@GMAIL.COM", "accounts":[ { "id":1, "balance":"$1000", "custid":"1", "accNbr

如何为下面结合了字符串和数组的JSON编写mockMVC测试

{
  "id":1,
  "firstName":"NPA",
  "lastName":"TAS",
  "mobile":"123454321",
  "email":"ABCD@GMAIL.COM",
  "accounts":[
         {
          "id":1,
          "balance":"$1000",
          "custid":"1",
          "accNbr":"12345"
         }
            ]
}
我的代码:

@Test
        public void testJson() throws Exception {
            mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
            mockMvc.perform(get("/acc/1")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.accounts.id", Matchers.is(1)))
            .andExpect(jsonPath("$.accounts.balance", Matchers.is("$1000")))
            .andExpect(jsonPath("$.accounts.accNbr", Matchers.is("12345")))
            .andExpect(jsonPath("$.accounts.custid", Matchers.is("1")))
            .andExpect(jsonPath("$.*", Matchers.hasSize(4)));
        }
我有个例外

JSON路径“$.accounts.id”处无值,异常:

应在路径$中找到属性为['accounts']的对象,但找到了'net.minidev.json.JSONArray'。根据JsonProvider的说法,这不是json对象:“com.jayway.jsonpath.spi.json.JsonSmartJsonProvider”

但是,如果我尝试使用$.accounts[0].id,则会出现异常

JSON路径“$.accounts[0].id”处没有值,异常:

应在路径$中找到属性为['accounts']的对象,但找到了'net.minidev.json.JSONArray'。根据JsonProvider的说法,这不是json对象:“com.jayway.jsonpath.spi.json.JsonSmartJsonProvider”


accounts
属性是一个数组,因此:
$.accounts.id
必须使用索引器,例如:
$.accounts[0].id

从:

[(,)]数组索引
如果不确定使用哪个索引,那么可以过滤JSON并在过滤后的account子文档上断言。例如:

  • $.accounts[?(@.id==1)]。余额
    :返回
    $1000
  • $.accounts[?(@.accNbr==12345)].id
    :返回
    1
  • 。。。等

中有更多的详细信息,您可以使用来处理此问题。

正如@glytching和我提到的,有一个数组,它应该与
$.accounts[0].id一起使用

如果您仍然遇到问题,我将尝试在您的控制台上打印结果:

MvcResult result = mockMvc.perform(MockMvcRequestBuilders
         .get("/acc/1").accept(MediaType.APPLICATION_JSON)).andReturn();

String content = result.getResponse().getContentAsString();

因为有一个帐户数组。因此,您应该使用$.accounts[0].id作为数组accounts中的第一个帐户。@KeyMarker00$.accounts[0].id也不起作用如果您将问题中提供的JSON与此表达式
$.accounts[0].id
一起使用,您将得到一个有效的答案,因此最有可能的解释是:“@KeyMarker00$.accounts[0].id也不起作用”是指
get(“/acc/1”)
生成的JSON与您在问题中包含的JSON不匹配。我怀疑该端点生成的actula JSON可能如下所示:
[{“id”:1,“firstName”:“NPA”,…}]
,即一个子文档数组,每个子文档包含一个accounts[],而不是一个包含accounts[]的文档。
MvcResult result = mockMvc.perform(MockMvcRequestBuilders
         .get("/acc/1").accept(MediaType.APPLICATION_JSON)).andReturn();

String content = result.getResponse().getContentAsString();