Skip to main content

JUnit 6 Best Practices

To upgrade to JUnit 6 and apply all associated best practices, you can use the OpenRewrite recipe org.openrewrite.java.testing.junit.JUnit6BestPractices.

This recipe applies the following changes:

  • JUnit 5 to 6 migration -- updates dependencies and removes APIs dropped in JUnit 6
  • Removes @EnabledOnJre/@DisabledOnJre conditions for Java versions below 17, since JUnit 6 requires Java 17+
  • Applies JUnit Jupiter best practices -- removes public modifiers, removes test prefixes, simplifies throws declarations
  • Updates assertions to use the latest recommended patterns
tip

Before running this recipe, ensure your project already compiles with Java 17+. The recipe migrates your test code but does not change your Java version.

You can run the migration recipe using one of the following methods.

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/
  2. 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
  3. Run the recipe.
    shell
    mod run ~/workspace/ --recipe org.openrewrite.java.testing.junit.JUnit6BestPractices