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.
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.
- Moderne CLI
- Maven Command Line
- Maven POM
- Gradle init script
- Gradle
- IntelliJ IDEA Ultimate
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.
- 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:
mod build ~/workspace/
- If the recipe is not available locally yet, then you can install it once using:
mod config recipes jar install org.openrewrite.recipe:rewrite-testing-frameworks:LATEST
- Run the recipe.
mod run ~/workspace/ --recipe org.openrewrite.java.testing.assertj.Assertj
You will need to have Maven installed on your machine before you can run the following command.
mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-testing-frameworks:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.testing.assertj.Assertj -Drewrite.exportDatatables=true
You may add the plugin to your pom.xml
file, so that it is available for all developers and CI/CD pipelines.
- Add the following to your
pom.xml
file:
<project>
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>LATEST</version>
<configuration>
<exportDatatables>true</exportDatatables>
<activeRecipes>
<recipe>org.openrewrite.java.testing.assertj.Assertj</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-testing-frameworks</artifactId>
<version>LATEST</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
- Run the recipe.
mvn rewrite:run
Gradle init scripts are a good way to try out a recipe without modifying your build.gradle
file.
- Create a file named
init.gradle
in the root of your project.
initscript {
repositories {
maven { url "https://plugins.gradle.org/m2" }
}
dependencies { classpath("org.openrewrite:plugin:latest.release") }
}
rootProject {
plugins.apply(org.openrewrite.gradle.RewritePlugin)
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:latest.release")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.assertj.Assertj")
setExportDatatables(true)
}
afterEvaluate {
if (repositories.isEmpty()) {
repositories {
mavenCentral()
}
}
}
}
- Run the recipe.
gradle --init-script init.gradle rewriteRun
You can add the plugin to your build.gradle
file, so that it is available for all developers and CI/CD pipelines.
- Add the following to your
build.gradle
file:
plugins {
id("org.openrewrite.rewrite") version("latest.release")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.assertj.Assertj")
setExportDatatables(true)
}
repositories {
mavenCentral()
}
dependencies {
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks:latest.release")
}
- Run
gradle rewriteRun
to run the recipe.
You can run OpenRewrite recipes directly from IntelliJ IDEA Ultimate, by adding a rewrite.yml
file to your project.
---
type: specs.openrewrite.org/v1beta/recipe
name: com.github.timtebeek.AdoptAssertJ
displayName: Adopt AssertJ
description: Adopt AssertJ and apply best practices to assertions.
recipeList:
- org.openrewrite.java.testing.assertj.Assertj
After adding the file, you should see a run icon in the left margin offering to run the recipe.
Exercise A
Run the above command on the books
project in this repository, and check the changes made to the test files.
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.
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.