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

Rename AssertableMockMvc to MvcTester and review assertions structure #32712

Closed
snicoll opened this issue Apr 26, 2024 · 1 comment
Closed

Rename AssertableMockMvc to MvcTester and review assertions structure #32712

snicoll opened this issue Apr 26, 2024 · 1 comment
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@snicoll
Copy link
Member

snicoll commented Apr 26, 2024

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:

  • The request and the response
  • What handler handled the request, and the ModelAndView if any
  • A resolved exception if the processing failed and an exception handled handled it

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 snicoll added status: waiting-for-triage An issue we've not yet triaged or decided on in: test Issues in the test module labels Apr 26, 2024
@jhoeller jhoeller changed the title Consier renaming and/or restructuring AssertableMvcResult Consider renaming and/or restructuring AssertableMvcResult Apr 26, 2024
@snicoll
Copy link
Member Author

snicoll commented May 7, 2024

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

  • Entry point named MockMvcTester (formerly known as AssertableMockMvc). After having considered several names and the fact that a reference to MockMvc and AssertJ can be desirable, we rather think that a pattern for the 2024 API review of things (à la *Client for production code) could be a way out. We think that *Tester could be such suffix. We still want to hint at MockMvc even if the actual implementation is mostly hidden (so it's rather a conceptual reference)
  • Result MvcTestResult (formerly AssertableMvcResult) It doesn't extend from MvcResult but provides access to it. That's actually very important as folks upgrading to the new infrastructure may keep a reference to MvcResult that would have compiled but not giving any AssertJ capable support then.

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

@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels May 7, 2024
@snicoll snicoll self-assigned this May 7, 2024
@snicoll snicoll added this to the 6.2.0-M2 milestone May 7, 2024
@snicoll snicoll changed the title Consider renaming and/or restructuring AssertableMvcResult Rename AssertableMockMvc to MvcTester and review assertions structure May 7, 2024
snicoll added a commit that referenced this issue May 7, 2024
This commit merges the JSONCompare and JsonPath support in a single
assert object. The rationale for the previous situation was that
JsonPath is optional but merging the assertions methods do not make
it mandatory as the usage if JsonPath is triggered only if it is
actually used.

See gh-32712
snicoll added a commit that referenced this issue May 7, 2024
This commit removes ResponseBodyAssert and rather offers first-class
access support for the response body at the root level using bodyText(),
bodyJson(), and body().

This avoids a double navigation to assert the response body.

See gh-32712
snicoll added a commit that referenced this issue May 7, 2024
This commit renames AssertableMockMvc to MockMvcTester as the former
was too mouthful, and we are looking for a naming pattern that can be
applied consistently to test utilities that rely on AssertJ.

Rather than AssertableMvcResult, we now use MvcTestResult and it no
longer extends from MvcResult itself. That avoids having existing code
that is migrated to the new API whilst assigning the result to MvcResult
and get a bad DevXP as that doesn't bring any of the AssertJ support.

See gh-32712
@snicoll snicoll closed this as completed in 168276a May 7, 2024
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

1 participant