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

Extend HTTP status assertions #34255

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cms04
Copy link

@cms04 cms04 commented Jan 13, 2025

This is an extension for HTTP status assertions in the AbstractHttpServletResponseAssert, which was introduced in version 6.2. With this extension, it is possible to write test assertions like this

MvcTestResult result = mockMvcTester.get().uri("/example").exchange();
assertThat(result).httpStatus().isUnauthorized();

which is inspired by the "old" MockMvc status assertions:

mockMvc.perform(get("/example"))
    .andExpect(status().isUnauthorized());

No breaking changes introduced.

- Introduce HttpStatusAssert in AbstractHttpServletResponseAssert

Signed-off-by: Christian Morbach <Christian-Morbach@hotmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jan 13, 2025
@snicoll
Copy link
Member

snicoll commented Jan 14, 2025

Thanks for the suggestion @cms04 - We're aware of the lack of support for HttpStatus and did not introduce them on purpose. One reason is to avoid exactly what you're suggesting. By having such assertions there, it breaks the flow if you're trying to assert simple use cases as httpStatus() returns a dedicated assert object.

The "root" assertion object has basic hasStatus methods. I am still not keen to add more there as it would become quite busy in such a high-level object. Taking a step back, why are such narrowed assertions necessary? assertThat(result).httpStatus().isUnauthorized() can be written as assertThat(result).hasStatus(HttpStatus.UNAUTHORIZED) right now and I prefer the latter over the former.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants