JUnit Jupiter best practices
Recipe ID
org.openrewrite.java.testing.junit.JupiterBestPracticesArtifact
org.openrewrite.recipe:rewrite-testing-frameworksApplies best practices to tests.
Usage
This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.
mod run . --recipe org.openrewrite.java.testing.junit.JupiterBestPracticesIf the recipe isn’t available locally, install it with:
mod config recipes jar install org.openrewrite.recipe:rewrite-testing-frameworks:3.43.0Definition
This recipe runs the following recipes in order.
- Migrate Hamcrest assertions to JUnit Jupiter
org.openrewrite.java.testing.hamcrest.MigrateHamcrestToJUnit5 - Statically import JUnit Jupiter assertions
org.openrewrite.java.testing.junit5.StaticImports - Remove JUnit 5 static Assertions imports in Kotlin when wildcard import is present
org.openrewrite.java.testing.junit5.CleanupKotlinJUnit5AssertionImports - Clean Up Assertions
org.openrewrite.java.testing.junit5.CleanupAssertions - Replace
@CsvSourcewith@ValueSourcefor single method argumentsorg.openrewrite.java.testing.junit5.CsvSourceToValueSource - Remove unnecessary
assertEqualsdelta argument for integral typesorg.openrewrite.java.testing.cleanup.AssertEqualsIntegralDeltaToAssertEquals - Replace JUnit
assertTrue(false)andassertFalse(true)withfail()org.openrewrite.java.testing.cleanup.AssertLiteralBooleanToFailRecipes - Remove JUnit
assertTrue(true)andassertFalse(false)org.openrewrite.java.testing.cleanup.AssertLiteralBooleanRemovedRecipe - Kotlin test methods should have return type
Unitorg.openrewrite.java.testing.cleanup.KotlinTestMethodsShouldReturnUnit - Remove
testprefix from JUnit 5 testsorg.openrewrite.java.testing.cleanup.RemoveTestPrefix - Simplify
throwsstatements of testsorg.openrewrite.java.testing.cleanup.SimplifyTestThrows - Remove
publicvisibility of JUnit 5 testsorg.openrewrite.java.testing.cleanup.TestsShouldNotBePublic - Test methods should have void return type
org.openrewrite.java.testing.cleanup.TestMethodsShouldBeVoid - Add missing
@ParameterizedTestannotation when@ValueSourceis used or replace@Testwith@ParameterizedTestorg.openrewrite.java.testing.junit5.AddParameterizedTestAnnotation - Remove duplicates uses of @TestTemplate implementations for a single method
org.openrewrite.java.testing.junit5.RemoveDuplicateTestTemplates - Replace
fail()intry-catchblocks withAssertions.assertDoesNotThrow(() -> { ... })org.openrewrite.java.testing.junit5.RemoveTryCatchFailBlocks - Make lifecycle methods non private
org.openrewrite.java.testing.junit5.LifecycleNonPrivate - Applies JUnit 5
assertThrowson last statement in lambda block onlyorg.openrewrite.java.testing.junit5.AssertThrowsOnLastStatement assertTrue(x instanceof y)toassertInstanceOf(y.class, x)org.openrewrite.java.testing.junit5.AssertTrueInstanceofToAssertInstanceOf- Make implausibly long
@Timeoutvalues explicit in minutesorg.openrewrite.java.testing.junit5.ImplausibleTimeoutToMinutes - Use JUnit5's
assertSameorassertNotSameinstead ofassertTrue(... == ...)org.openrewrite.java.testing.junit5.UseAssertSame - Remove JUnit Jupiter migrationsupport
org.openrewrite.java.testing.junit.RemoveJupiterMigrationSupport
Data tables
Structured output this recipe can produce.
- Source files that had resultsSource files that were modified by the recipe run.
org.openrewrite.table.SourcesFileResults - Source files that had search resultsSearch results that were found during the recipe run.
org.openrewrite.table.SearchResults - Source files that errored on a recipeThe details of all errors produced by a recipe run.
org.openrewrite.table.SourcesFileErrors - Recipe performanceStatistics used in analyzing the performance of recipes.
org.openrewrite.table.RecipeRunStats