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

@ExtendWith and @ContextConfguration can be simplified to @SpringJUnitConfig #296

Closed
nbruno opened this issue Feb 26, 2023 · 5 comments · Fixed by #297 or #298
Closed

@ExtendWith and @ContextConfguration can be simplified to @SpringJUnitConfig #296

nbruno opened this issue Feb 26, 2023 · 5 comments · Fixed by #297 or #298
Labels
good first issue Good for newcomers recipe Recipe requested

Comments

@nbruno
Copy link
Contributor

nbruno commented Feb 26, 2023

Spring introduced the @SpringJUnitConfig annotation as a composed annotation for @ExtendWith(SpringExtension.class) and @ContextConfiguration. A recipe should be created to perform this migration.

The recipe should transform the following code:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = ExampleConfiguration.class)

Into:

@SpringJUnitConfig(classes = ExampleConfiguration.class)

Note that @SpringJUnitConfig contains aliases for the following @ContextConfiguration attributes which should be maintained from the original @ContextConfiguration annotation when creating the @SpringJUnitConfig annotation:

  • value
  • classes
  • locations
  • initializers
  • inheritLocations
  • inheritInitializers
  • name

It would be beneficial to also include this in the migration from JUnit4 to JUnit5 recipe, possibly as another step of the recipe that removes unnecessary @RunWith annotations or as a top-level recipe addition to the migration recipe.

@timtebeek timtebeek added recipe Recipe requested good first issue Good for newcomers labels Feb 26, 2023
@timtebeek
Copy link
Contributor

Nice suggestion for another cleanup indeed!

nbruno added a commit to nbruno/rewrite-spring that referenced this issue Feb 26, 2023
timtebeek added a commit that referenced this issue Feb 27, 2023
* Add recipe to replace @ExtendWith and @ContextConfiguration with @SpringJUnitConfig (#296)

* Override getSingleSourceApplicableTest for UsesType checks

Also apply default formatter for consistency

* Adopt String.formatted instead of String concatenation

* Consistently use replace with instead of into

* Adopt Applicability.and(..) to require both annotations

* Move and temporarily disable inclusion in update chain

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
@timtebeek timtebeek reopened this Feb 27, 2023
@timtebeek
Copy link
Contributor

Reopened as per the final comment on #297 (comment). We want to try this out on a number of Spring projects through public.moderne.io, so ensure there are no edge cases left uncovered before we hook this into the chain of upgrade recipes by default.

@timtebeek
Copy link
Contributor

@nbruno I've just updated the platform to include your recipe: https://public.moderne.io/recipes/org.openrewrite.java.spring.boot2.ReplaceExtendWithAndContextConfiguration

When I run that recipe in isolation against 49 Spring projects, you get the following results: https://public.moderne.io/results/kfx9I
image

For the top one you can then create a fork, as I've done, to check that out and confirm it's working locally. Once confirmed you can then create a pull request to contribute back to Spring if you'd like.

Also let me know if you'd like me to include any of your own (public) projects in the platform to verify the recipe!

Once we have a decent sampling covered we can hook the recipe up for inclusion in the default migration chain, as discussed.

@timtebeek
Copy link
Contributor

timtebeek commented Feb 27, 2023

Confirmed to work locally for spring-data-jpa already: promising!
Won't open a pull request there though as it's your work, not mine. :)

@nbruno
Copy link
Contributor Author

nbruno commented Feb 28, 2023

Awesome! Opened the draft PR to enable this in the default migration chain. Feel free to submit PR's to the Spring repositories if it'd be beneficial for them.

timtebeek added a commit that referenced this issue Mar 2, 2023
* Enable recipe in JUnit 5 migration (#296)

* Enable test associated with SpringJUnitConfig

---------

Co-authored-by: Tim te Beek <tim@moderne.io>
@rpau rpau added this to the Support Spring Framework migrations milestone May 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers recipe Recipe requested
Projects
Archived in project
3 participants