预期:null 但是:是 <[null]> - Hamcrest 和 jsonPath
Expected: null but: was <[null]> - Hamcrest and jsonPath
我想断言休息控制器的 json 输出,但我得到 "Expected: null but: was <[null]>"。这是我的测试代码:
mockMvc.perform(post(TEST_ENDPOINT)
.param("someParam", SOMEPARAM)
.andDo(print())
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("*.errorMessage").value(IsNull.nullValue()));
Json:
{
"some_string": {
"errorMessage": null
}
}
我发现了类似的问题 How to assertThat something is null with Hamcrest?,但两个答案都不起作用。也许这是由于 jsonPath,导致它在 [] 括号中 return 空值?是断言框架的bug吗?
JSON根据文档,路径总是 return 一个数组,
Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it.
根据此处的结果部分 [JSONPath - JSON] 的 XPath:(http://goessner.net/articles/JsonPath/index.html)
这样就排除了
的任何问题
cause it return null value in [] brackets
nullValue 应按如下方式工作
import static org.hamcrest.CoreMatchers.nullValue;
....
.andExpect(jsonPath("some_string.errorMessage", nullValue()))
或
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
....
.andExpect(jsonPath("some_string.errorMessage", is(nullValue())))
如此处找到的答案中所述How to assertThat something is null with Hamcrest?
我想断言休息控制器的 json 输出,但我得到 "Expected: null but: was <[null]>"。这是我的测试代码:
mockMvc.perform(post(TEST_ENDPOINT)
.param("someParam", SOMEPARAM)
.andDo(print())
.andExpect(status().is2xxSuccessful())
.andExpect(jsonPath("*.errorMessage").value(IsNull.nullValue()));
Json:
{
"some_string": {
"errorMessage": null
}
}
我发现了类似的问题 How to assertThat something is null with Hamcrest?,但两个答案都不起作用。也许这是由于 jsonPath,导致它在 [] 括号中 return 空值?是断言框架的bug吗?
JSON根据文档,路径总是 return 一个数组,
Please note, that the return value of jsonPath is an array, which is also a valid JSON structure. So you might want to apply jsonPath to the resulting structure again or use one of your favorite array methods as sort with it.
根据此处的结果部分 [JSONPath - JSON] 的 XPath:(http://goessner.net/articles/JsonPath/index.html)
这样就排除了
的任何问题cause it return null value in [] brackets
nullValue 应按如下方式工作
import static org.hamcrest.CoreMatchers.nullValue;
....
.andExpect(jsonPath("some_string.errorMessage", nullValue()))
或
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
....
.andExpect(jsonPath("some_string.errorMessage", is(nullValue())))
如此处找到的答案中所述How to assertThat something is null with Hamcrest?