【问题标题】:No value at JSON path "$"JSON 路径“$”处没有值
【发布时间】:2018-03-02 07:04:35
【问题描述】:

我正在测试休息控制器。这是测试代码:

mockMvc.perform(get("/index/get-all"))
                .andExpect(status().isOk())
                .andDo(print())
                .andExpect(jsonPath("$",hasSize(2)));

我得到响应正文:

Body = [{"id":"123"},{"id":"1234"}]

我得到错误:

java.lang.AssertionError: No value at JSON path "$", exception: net/minidev/json/writer/JsonReaderI

我做错了什么?

【问题讨论】:

    标签: spring junit jsonpath


    【解决方案1】:

    你的身体返回一个包含对象的数组。要访问 Spring MVC 测试中的每个对象,请使用以下断言:

    .andExpect(jsonPath("[0].id").value("123"))
    .andExpect(jsonPath("[1].id").value("1234"))
    

    【讨论】:

      【解决方案2】:

      得到同样的错误。 尝试在您的依赖项中使用更高版本的 json-smart,并从包含 json-smart 的包中排除较低版本。对我来说,我将依赖项更改为:

          <dependency>
              <groupId>com.jayway.jsonpath</groupId>
              <artifactId>json-path</artifactId>
              <version>2.2.0</version>
              <scope>test</scope>
              <exclusions>
                  <exclusion>
                      <artifactId>json-smart</artifactId>
                      <groupId>net.minidev</groupId>
                  </exclusion>
              </exclusions>
          </dependency>
          <dependency>
              <groupId>com.jayway.jsonpath</groupId>
              <artifactId>json-path-assert</artifactId>
              <version>2.2.0</version>
              <scope>test</scope>
          </dependency>
          <dependency>
              <groupId>net.minidev</groupId>
              <artifactId>json-smart</artifactId>
              <version>2.2</version>
              <scope>test</scope>
          </dependency>
      

      【讨论】:

        【解决方案3】:

        仔细检查您的网址:“/index/get-all”并确保您使用的是控制器类的完整路径。我遇到了同样的问题,因为我的 url 缺少路径的一部分(“/Folder/”)。我一直使用的是“/folderId=1234”,并意识到我忘记在类的顶部添加控制器的@RequestMapping 注释部分,所以它应该是“/Folder/folderId=1234”。

        示例控制器代码

        @RestController
        @RequestMapping ("/Folder")
        public class FolderController {
        
        @RequestMapping (value = "/folderId={Id}", method = RequestMethod.GET)
            public Folder getFolderById (@PathVariable String folderId, HttpSession session)
            {
               // controller code
            }
        }
        

        我使用的网址不正确。

        mockMvc.perform(
                                    get("/folderId=1234")
                                             .andExpect(status().isOk())
                                             .andDo(print())
                                             .andExpect(jsonPath("$",hasSize(2)));
        

        正确的网址。

        mockMvc.perform(
                                get("/Folder/folderId=1234")
                                         .andExpect(status().isOk())
                                         .andDo(print())
                                         .andExpect(jsonPath("$",hasSize(2)));
        

        【讨论】:

          【解决方案4】:

          我有一个场景,堆栈跟踪显示类似于您的错误:

          java.lang.AssertionError: No value at JSON path "$[0].contentId"
          Caused by: java.lang.NoClassDefFoundError: net/minidev/json/writer/JsonReaderI
          

          我发现这篇文章:https://github.com/json-path/JsonPath/issues/159#issuecomment-411306322 引起了我的注意。我在我的项目中使用了 net.minidev:json-smart:1.1.1。

          我所做的只是将版本更新到 2.3,问题就解决了。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2019-06-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-02-13
            • 1970-01-01
            相关资源
            最近更新 更多