When using MockMvc
the status reason phrase can be asserted like this:
mockMvc
.perform(get("/something"))
.andExpect(status().isNotFound())
.andExpect(status().reason("Could not find xyz"))
This cannot be achieved using MockMvcTester
:
val result = mockMvc.get().uri("/something")
assertThat(result)
.hasStatus(HttpStatus.NOT_FOUND)
// ??? How to assert the status reason phrase
Is there something I'm missing?
Comment From: snicoll
Thanks for the report. I don't think you're missing anything, except the reason
bit is a bit misleading. It's actually the error message that's available only when HttpServletResponse#sendError(int, String)
is called.
We don't offer a direct assertion for that and we need to figure out if we want. In the meantime you can use the following:
assertThat(result.getResponse().getErrorMessage()).isEqualTo("Could not find xyz");
Comment From: bclozel
I went ahead and added a mvc.get().uri("/user/42")).hasErrorMessage("error message")
assertion for this.
This is not a common use case, but servletResponse.sendError(int, String)
is still called by Spring Framework in some features like @ResponseStatus(code = HttpStatus.BAD_REQUEST, reason = "my error message")
. While it's referred as "status reason" in this case, this message is not sent on the HTTP response status line but is rather a Servlet error message dedicated to Servlet error pages. I believe hasErrorMessage
and the companion Javadoc fits that description.