Links

Migrate JUnit Asserts to AssertJ

In this tutorial, we'll use OpenRewrite to perform an automated migration from JUnit Asserts to AssertJ. AssertJ is a Java library that provides a rich set of assertions and truly helpful error messages, improves test code readability, and is designed to be super easy to use within your favorite IDE.

Example Configuration

The migrate to AssertJ recipe can be applied by adding OpenRewrite's plugin to your project and including a dependency on rewrite-testing-frameworks:
Maven
Gradle
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.23.1</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.testing.assertj.JUnitToAssertj</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-testing-frameworks</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<build>
build.gradle
plugins {
id("java")
id("org.openrewrite.rewrite") version("6.8.4")
}
rewrite {
activeRecipe("org.openrewrite.java.testing.assertj.JUnitToAssertj")
}
repositories {
mavenCentral() // rewrite-spring is published to Maven Central
}
dependencies {
rewrite(platform("org.openrewrite.recipe:rewrite-recipe-bom:2.7.1"))
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks")
// Other project dependencies
}
At this point, you're ready to execute the migration by running mvn rewrite:run or gradlew rewriteRun. After running the migration you can inspect the results with git diff (or equivalent), manually fix anything that wasn't able to be migrated automatically, and commit the results.

Before and After

For the full list of changes, this recipe will make, see its reference page.
JUnit Assert Test Class (Before)
AssertJ Test Class (After)
package org.openrewrite.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class ExampleJunitTestClass {
@Test
void someTest() {
try {
QueryRegistry queryRegistry = new QueryRegistry.Builder().build();
fail();
} catch (IllegalArgumentException ignored) {
}
// ...
assertEquals(1, currentSubs.size());
assertEquals("myPrefix_subId", currentSubs.get(0).getSubscriptionId());
assertTrue(allSubscriptions.containsKey("myApp"));
}
@Test
void shouldFailToInitializeWithMissingTable() {
when(catalog.loadTable(any())).thenThrow(new RuntimeException());
IcebergWriterStage stage = new IcebergWriterStage();
assertThrows(RuntimeException.class, () -> stage.init(context));
}
}
package org.openrewrite.example;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
public class ExampleJunitTestClass {
@Test
void someTest() {
try {
QueryRegistry queryRegistry = new QueryRegistry.Builder().build();
fail("");
} catch (IllegalArgumentException ignored) {
}
// ...
assertThat(currentSubs.size()).isEqualTo(1);
assertThat(currentSubs.get(0).getSubscriptionId()).isEqualTo("myPrefix_subId");
assertThat(allSubscriptions.containsKey("myApp")).isTrue();
}
@Test
void shouldFailToInitializeWithMissingTable() {
when(catalog.loadTable(any())).thenThrow(new RuntimeException());
IcebergWriterStage stage = new IcebergWriterStage();
assertThatExceptionOfType(RuntimeException.class).isThrownBy(() -> stage.init(context));
}
}