Skip to content

Commit

Permalink
Add a Junit asserts to AssertJ guide
Browse files Browse the repository at this point in the history
Fixes: #53

Very rudimentary guide -- just ran on the default group and saw what
changes were made. Could potentially use more in the future
  • Loading branch information
mike-solomon committed Dec 13, 2023
1 parent b0aae8a commit a76661d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
1 change: 1 addition & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* [Refactoring with declarative YAML recipes](running-recipes/popular-recipe-guides/authoring-declarative-yaml-recipes.md)
* [Automating Maven dependency management](running-recipes/popular-recipe-guides/automating-maven-dependency-management.md)
* [Migrate Hamcrest to AssertJ](running-recipes/popular-recipe-guides/hamcrest-to-assertj.md)
* [Migrate JUnit Asserts to AssertJ](/running-recipes/popular-recipe-guides/junit-asserts-to-assertj.md)
* [Authoring Recipes](authoring-recipes/authoring-recipes.md)
* [Recipe development environment](authoring-recipes/recipe-development-environment.md)
* [The different types of recipes](authoring-recipes/types-of-recipes.md)
Expand Down
143 changes: 143 additions & 0 deletions running-recipes/popular-recipe-guides/junit-asserts-to-assertj.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Migrate to AssertJ from JUnit Asserts

In this tutorial, we'll use OpenRewrite to perform an automated migration from [JUnit Asserts](https://junit.org/junit4/javadoc/4.13/org/junit/Assert.html) to [AssertJ](https://assertj.github.io/doc/). 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](https://github.com/openrewrite/rewrite-testing-frameworks):

{% tabs %}
{% tab title="Maven" %}
{% code title="pom.xml" %}
```xml
<build>
<plugins>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.15.4</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.1.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<build>
```
{% endcode %}
{% endtab %}

{% tab title="Gradle" %}
{% code title="build.gradle" %}
```groovy
plugins {
id("java")
id("org.openrewrite.rewrite") version("6.5.12")
}
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.5.3"))
rewrite("org.openrewrite.recipe:rewrite-testing-frameworks")
// Other project dependencies
}
```
{% endcode %}
{% endtab %}
{% endtabs %}

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](/reference/recipes/java/testing/assertj/junittoassertj.md).

{% tabs %}
{% tab title="JUnit Assert Test Class (Before)" %}
```java
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));
}
}
```
{% endtab %}

{% tab title="AssertJ Test Class (After)" %}
```java
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));
}
}
```
{% endtab %}
{% endtabs %}
1 change: 1 addition & 0 deletions running-recipes/running-recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
* [Refactoring with declarative YAML recipes](/running-recipes/popular-recipe-guides/authoring-declarative-yaml-recipes.md)
* [Automating Maven dependency management](/running-recipes/popular-recipe-guides/automating-maven-dependency-management.md)
* [Migrate Hamcrest to AssertJ](/running-recipes/popular-recipe-guides/hamcrest-to-assertj.md)
* [Migrate JUnit Asserts to AssertJ](/running-recipes/popular-recipe-guides/junit-asserts-to-assertj.md)

0 comments on commit a76661d

Please sign in to comment.