java 断言错误:JUnit 测试中的 JSON 路径没有值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33659684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:57:06  来源:igfitidea点击:

Assertion error: No value for JSON Path in JUnit test

javajsonjunitspring-mvc-test

提问by Robin van Aalst

I have written a test and it succeeded before but now I get an AssertionError: No value for JSON Path.

我已经写了一个测试,它之前成功了,但现在我得到一个 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));
}

The error I get is:

我得到的错误是:

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

I don't understand what it is not getting or what is missing.

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

回答by Mathias Dpunkt

You are asserting that your response contains a field namewith value Bordeaux.

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

You can print your response using this.webClient.perform(...).andDo(print()).

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

回答by Nagesh Dalave

I was getting same problem.

我遇到了同样的问题。

Solution :

解决方案 :

Use .andReturn().getResponse().getContentAsString();, your response will be a string. My response was:

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

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

When I was trying to do .andExpect(jsonPath("$.id", is(1)));there was an error: java.lang.AssertionError: No value for JSON path: $.id

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

To fix it, I did .andExpect(jsonPath("$.data.id", is(1)));and it works because id is a field in data.

为了修复它,我做到了.andExpect(jsonPath("$.data.id", is(1)));并且它有效,因为 id 是数据中的一个字段。

回答by Marko Markovic

Most likely jsonPath interprets the body of your file as a list and this should do the trick (mind the added square brackets as list accessors):

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

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

回答by Yurii Malskiy

I had the same problem after adding Hymanson-dataformat-xmldependency to my project.

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

To solve this i had to change my response entity from:

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

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

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

to

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

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

In this way it will work fine, cause you've directly set the jsonas return type for your body.

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

回答by Yurii Malskiy

Whatever you are testing for .namedoes not have a property called nameanymore the error message is pretty clear about that part.

无论您要测试什么,都.name不再有一个被调用的属性name,错误消息对该部分非常清楚。

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

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

No one but you knows what you changed to make it go from workingto not workingnothing you posted in your question can tell us that.

除了您之外,没有人知道您进行了哪些更改以使其从工作变为不工作,您在问题中发布的任何内容都无法告诉我们。