I'm trying to test an Spring MVC controller's error response with the following
this.mockMvc
.perform(get("/healthChecks/9999"))
.andExpect(status().isNotFound())
.andExpect(jsonPath('error', is("Not Found")))
.andExpect(jsonPath('timestamp', is(notNullValue())))
.andExpect(jsonPath('status', is(404)))
.andExpect(jsonPath('path', is(notNullValue())))
but the test is failing with the exception:
java.lang.IllegalArgumentException: json can not be null or empty
on the first jsonPath assertion.
When I curl this URL, I get the following:
{
"timestamp": 1470242437578,
"status": 200,
"error": "OK",
"exception": "gov.noaa.ncei.gis.web.HealthCheckNotFoundException",
"message": "could not find HealthCheck 9999.",
"path": "/healthChecks/9999"
}
Can someone please help me understand what I'm doing wrong?
You write incorrect JsonPath expressions (For More Information LINK), use $
for root path. In your case it should look like:
.andExpect(jsonPath('$.error', is("Not Found")))
.andExpect(jsonPath('$.timestamp', is(notNullValue())))
.andExpect(jsonPath('$.status', is(404)))
.andExpect(jsonPath('$.path', is(notNullValue())))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With