Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC 5 ResultMatcher jsonPath null value

After upgrading my rest service from Spring Boot 1.5.10 to 2.0.0 I encountered my tests failing which passed before.

Following Scenario:

import org.mockito.internal.matchers.Null;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;

...

.andExpect(jsonPath("img").value(Null.NULL))

Fails now in Spring MVC 5 with following message:

java.lang.AssertionError: JSON path "img"
Expected :isNull() Actual :null

What is the correct way in Spring MVC 5 to assert that the value of the jsonPath is null?

like image 237
mrkernelpanic Avatar asked Mar 14 '18 14:03

mrkernelpanic


Video Answer


1 Answers

Answering my own question as I found the solution by myself.

You have to use the correct Matcher, in my case org.hamcrest.core.IsNull

So I had to change to

import org.hamcrest.core.IsNull;
...
andExpect(jsonPath("img").value(IsNull.nullValue()))
like image 142
mrkernelpanic Avatar answered Sep 20 '22 18:09

mrkernelpanic