Converting your JUnit 5 assertions to AssertJ

This page provides a script which converts your existing JUnit 5 assertions to AssertJ ones. Note that both types of assertions can coexist, you don't have to migrate all at once.

The idea is to convert code like :

assertEquals(expected, actual);
to :

assertThat(actual).isEqualTo(expected);

See the JUnit 4 Assertions Migration Guide for more information. This script is a copy of JUnit 4 script with few necessary modifications.

Currently there is only OSX script available for JUnit 5 :

Usage : On OSX execute the script in the base directory containing the test files :

# in the directory containing the test files
convert-junit5-assertions-to-assertj-on-osx.sh

It is not perfect but should do most of the job. After executing it, you will need to :

  • optimize imports with your IDE to remove unused imports.
  • if you were using assertEquals with a delta to compare numbers, you will need to statically import org.assertj.core.api.Assertions.offset which is how you express deltas in AssertJ (see the number_assertions_with_offset_examples() test at the end of NumberAssertionsExamples for an example).

The script handles the cases where you use an assertion description, for example :

assertEquals("a", "a", "test context");
will be replaced by :

assertThat("a").as("test context").isEqualTo("a");