【问题标题】:Assertion error: No value for JSON Path in JUnit test断言错误:JUnit 测试中的 JSON 路径没有值
【发布时间】:2016-02-13 01:59:56
【问题描述】:

我已经写了一个测试,之前它成功了,但现在我得到一个 AssertionError: No value for JSON Path。

@Test
public void testCreate() throws Exception {
    Wine wine = new Wine();
    wine.setName("Bordeaux");
    wine.setCost(BigDecimal.valueOf(10.55));

    new Expectations() {
        {
            wineService.create((WineDTO) any);
            result = wine;
        }
    };

    MockMultipartFile jsonFile = new MockMultipartFile("form", "", "application/json", "{\"name\":\"Bordeaux\", \"cost\": \"10.55\"}".getBytes());
    this.webClient.perform(MockMvcRequestBuilders.fileUpload("/wine").file(jsonFile))
            .andExpect(MockMvcResultMatchers.status().is(200))
            .andExpect(MockMvcResultMatchers.jsonPath("$.name").value("Bordeaux"))
            .andExpect(MockMvcResultMatchers.jsonPath("$.cost").value(10.55));
}

我得到的错误是:

java.lang.AssertionError: No value for JSON path: $.name, exception: No results path for $['name']

我不明白它没有得到什么或缺少什么。

【问题讨论】:

    标签: java json junit spring-mvc-test


    【解决方案1】:

    您断言您的响应包含一个字段name,其值为Bordeaux

    您可以使用this.webClient.perform(...).andDo(print()) 打印您的回复。

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。

      解决方案:

      使用.andReturn().getResponse().getContentAsString();,您的回复将是一个字符串。我的回答是:

      {"url":null,"status":200,"data":{"id":1,"contractName":"Test contract"}
      

      当我尝试执行.andExpect(jsonPath("$.id", is(1))); 时出现错误:java.lang.AssertionError: No value for JSON path: $.id

      为了解决这个问题,我做了.andExpect(jsonPath("$.data.id", is(1)));,它可以工作,因为 id 是数据中的一个字段。

      【讨论】:

        【解决方案3】:

        很可能 jsonPath 将文件的主体解释为列表,这应该可以解决问题(注意添加的方括号作为列表访问器):

        .andExpect(MockMvcResultMatchers.jsonPath("$[0].name").value("Bordeaux"))
        .andExpect(MockMvcResultMatchers.jsonPath("$[0].cost").value(10.55));
        

        【讨论】:

          【解决方案4】:

          我的返回正文是{'status': 'FINISHED', 'active':false} 并且 jsonPath 确实看到了 status 字段,但看到了 active。 解决办法:用jsonPath("$.['status']")代替jsonPath("$.status")

          我的假设:可能 jsonPath 忽略了一些关键字,如“状态”等......

          【讨论】:

            【解决方案5】:

            jackson-dataformat-xml 依赖项添加到我的项目后,我遇到了同样的问题。

            为了解决这个问题,我不得不改变我的响应实体:

            return new ResponseEntity<>(body, HttpStatus.*STATUS*)

            return ResponseEntity.*status*().contentType(MediaType.APPLICATION_JSON).body(*your body*).

            这样它就可以正常工作了,因为您已经直接将json 设置为您身体的返回类型。

            【讨论】:

              【解决方案6】:

              无论您为 .name 测试什么,都不再有一个名为 name 的属性,错误消息对此部分非常清楚。

              java.lang.AssertionError: No value for JSON path: $.name, exception: No results path for $['name']

              没有人知道你做了什么改变以使其从工作变为不工作你在问题​​中发布的任何内容都无法告诉我们。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-10-05
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-03-27
                相关资源
                最近更新 更多