openrewrite / openrewrite/rewrite-spring
Migrate MockMvc Hamcrest assertions over to AssertJ
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 403
- Forks
- 149
- Avg merge
- 2h 33m
- Merged PRs (30d)
- 10
Description
What problem are you trying to solve?
Spring Boot 3.4 added a more fluent way to write MockMvc assertions using AssertJ.
What precondition(s) should be checked before applying this recipe?
Using Hamcrest assertions.
Describe the situation before applying the recipe
// static import of MockMvcRequestBuilders.* and MockMvcResultMatchers.*
mockMvc.perform(get("/accounts/1")).andExpectAll(
status().isOk(),
content().contentType("application/json;charset=UTF-8"));
Describe the situation after applying the recipe
assertThat(mockMvc.get().uri("/hotels/{id}", 42))
.hasStatusOk()
.hasContentTypeCompatibleWith(MediaType.APPLICATION_JSON);
Have you considered any alternatives or workarounds?
There is also the form of mockMvc.perform to reuse more of the original request, while still using fluent assertions:
https://docs.spring.io/spring-framework/reference/testing/mockmvc/assertj/integration.html
// Static import on MockMvcRequestBuilders.get
assertThat(mockMvc.perform(get("/hotels/{id}", 42)))
.hasStatusOk();
// For custom matchers
// Static import on MockMvcResultMatchers.status
assertThat(mockMvc.get().uri("/hotels/{id}", 42))
.matches(status().isOk());
This is likely not preferred as it still requires additional static imports, but could be an option for some cases, or as an intermediate to migrate to before moving over completely.
Any additional context
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No implementation files or tests are named. Start by reading the MockMvc AssertJ, setup-options, and integration documentation linked in the issue, then inspect the repository's existing recipe entry points and tests for related Spring testing migrations. Done means the recipe handles the shown Hamcrest-to-AssertJ cases and has coverage for the supported alternatives.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, spring-boot
- Domain
- testing, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100