Skip to main content

Migrate to AssertJ

You can easily convert existing assertions to use AssertJ using OpenRewrite's AssertJ recipe. This recipe contains many Refaster rules from Error Prone Support. There are multiple ways to run OpenRewrite recipes on your project, depending on your build tool and preferences.

We recommend the Moderne CLI when interested in running recipes across multiple projects, or when running multiple recipes in succession. If you want to try out the recipe on a single project, you can also use the OSS Maven and Gradle plugins.

note

Building the Lossless Semantic Tree (LST) of your project will take some time, proportional to the size of your project. With the Moderne CLI this only needs to be done once, after which subsequent recipe runs will be much faster.

The Moderne CLI allows you to run OpenRewrite recipes on your project without needing to modify your build files, against serialized Lossless Semantic Tree (LST) of your project for a considerable performance boost & across projects.

You will need to have configured the Moderne CLI on your machine before you can run the following command.

  1. If project serialized Lossless Semantic Tree is not yet available locally, then build the LST. This is only needed the first time, or after extensive changes:
shell
mod build ~/workspace/
  1. If the recipe is not available locally yet, then you can install it once using:
shell
mod config recipes jar install org.openrewrite.recipe:rewrite-testing-frameworks:LATEST
  1. Run the recipe.
shell
mod run ~/workspace/ --recipe org.openrewrite.java.testing.assertj.Assertj

Exercise A

Run the above command on the books project in this repository, and check the changes made to the test files.

note

Notice how nearly all the bad and outdated practices have been replaced with AssertJ's fluent assertions. Existing use of AssertJ has been improved by for instance chaining assertions and removing redundant calls.

Exercise B

Run the above command on your own project, and check the changes made to the test files.

info

At this point you can choose whether you want to keep the changes or revert them. If you'd prefer a smaller set of changes, you can also try out individual recipes from the AssertJ recipe family.