将我的休息服务从Spring Boot 1.5.10升级到2.0.0后,我遇到了之前通过的测试失败。
以下情景:
import org.mockito.internal.matchers.Null;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
...
.andExpect(jsonPath("img").value(Null.NULL))
现在在Spring MVC 5中失败,并显示以下消息:
java.lang.AssertionError:JSON路径" img"
预期:isNull() 实际:null
Spring MVC 5中断言jsonPath
的值是null
的正确方法是什么?
答案 0 :(得分:2)
在我自己找到解决方案时回答我自己的问题。
在我的案例org.hamcrest.core.IsNull
所以我不得不改为
import org.hamcrest.core.IsNull;
...
andExpect(jsonPath("img").value(IsNull.nullValue()))
答案 1 :(得分:1)
你可以使用content()。srtring(Matcher matcher)然后使用IsEmptyString matcher
result.andDo(print())
.andExpect(status().isNoContent())
.andExpect(content().string(IsEmptyString.isEmptyOrNullString()));