spring-projects / spring-framework

Spring Framework
https://spring.io/projects/spring-framework
Apache License 2.0
55.28k stars 37.62k forks source link

Rename AssertableMockMvc to MvcTester and review assertions structure #32712

Closed snicoll closed 1 week ago

snicoll commented 2 weeks ago

When we perform a request with MockMvc, we get a MvcResult that contains all the elements we need to assert that the processing of the request went well, such as:

AssertableMvcResult expands on the latter by providing the unresolved exception as well, rather than throwing a checked exception as MockMvc does.

This class can then be fed to AssertJ and we can then navigate on various pieces of the result:

assertThat(result).hasStatusOk()
assertThat(result).body()...
assertThat(result).headers()...
assertThat(result).cookies()...
assertThat(result).model()...
assertThat(result).handler()...

There are two things we'd like to explore here:

  1. We want users to assign the result of perform so that they can run multiple asserts against the result. The name AssertableMvcResult is a bit off putting so we should consider renaming it
  2. If we did then we should investigate the possiblity of exposing a MvcResult than extending MvcResult. This may allow us to navigate to more structured part of the result, such as:
assertThat(result.body())...
assertThat(result.headers())...

paging @philwebb and @simonbasle who helped me brainstorm on this.

snicoll commented 1 week ago

We've made quite a bit of progress here, focusing on the renaming bit of it. We came to the following proposal:

Irrespective of this issue, we've reviewed of the API and found several shortcuts to be desirable, as well as avoid a double navigation for the body.

This proposal is available at https://github.com/snicoll/spring-framework/tree/mvctester-review