我已经编写了一个测试并且之前成功但现在我得到一个AssertionError:没有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']
我不明白它没有得到什么或者缺少什么。
答案 0 :(得分:6)
您声称您的回复包含值为name
的字段Bordeaux
。
您可以使用this.webClient.perform(...).andDo(print())
打印回复。
答案 1 :(得分:4)
我遇到了同样的问题。
解决方案:
使用.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是数据中的一个字段。
答案 2 :(得分:0)
无论您为(define-key shell-mode-map [(\C-z)] 'clear-shell-buffer)
;;Output: (define-key shell-mode-map [(C-z)] (quote clear-shell-buffer))
(define-key shell-mode-map [(?\C-z)] 'clear-shell-buffer)
;;Output: (define-key shell-mode-map [(26)] (quote clear-shell-buffer))
测试什么,都不再具有名为.name
的属性,错误消息非常清楚该部分。
name
除了你知道你改变了什么以使其从工作到无法正常工作没有人你在问题中发布的任何内容都可以告诉我们。
答案 3 :(得分:0)
最有可能的jsonPath将文件的主体解释为列表,这应该可以解决问题(注意添加的方括号作为列表访问器):
.andExpect(MockMvcResultMatchers.jsonPath("$[0].name").value("Bordeaux"))
.andExpect(MockMvcResultMatchers.jsonPath("$[0].cost").value(10.55));
答案 4 :(得分:0)
向项目添加jackson-dataformat-xml
依赖项后,我遇到了同样的问题。
要解决此问题,我必须将响应实体从以下位置更改:
return new ResponseEntity<>(body, HttpStatus.*STATUS*)
到
return ResponseEntity.*status*().contentType(MediaType.APPLICATION_JSON).body(*your body*)
。
这样可以正常工作,因为您已经直接将json
设置为身体的返回类型。