Spring MVC 5 ResultMatcher jsonPath null值

时间:2018-03-14 14:04:12

标签: spring spring-mvc spring-boot mockito spring-test

将我的休息服务从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的正确方法是什么?

2 个答案:

答案 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()));