Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot assert status reason phrase with MockMvcTester #34016

Closed
mahozad opened this issue Dec 4, 2024 · 2 comments
Closed

Cannot assert status reason phrase with MockMvcTester #34016

mahozad opened this issue Dec 4, 2024 · 2 comments
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@mahozad
Copy link

mahozad commented Dec 4, 2024

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?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 4, 2024
@snicoll
Copy link
Member

snicoll commented Dec 4, 2024

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");

@sbrannen sbrannen added in: test Issues in the test module in: web Issues in web modules (web, webmvc, webflux, websocket) labels Dec 4, 2024
@snicoll snicoll removed the in: web Issues in web modules (web, webmvc, webflux, websocket) label Dec 4, 2024
@bclozel bclozel self-assigned this Dec 10, 2024
@bclozel bclozel added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 11, 2024
@bclozel bclozel added this to the 6.2.1 milestone Dec 11, 2024
@bclozel
Copy link
Member

bclozel commented Dec 11, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

5 participants