JUnit Jupiter best practices
MSALApplies best practices to JUnit Jupiter tests, such as statically importing and simplifying assertions, removing the test prefix and public modifiers from tests, and removing the JUnit Jupiter migration support.
org. openrewrite. java. testing. junit. JupiterBestPracticesmod run . --recipe= org. openrewrite. java. testing. junit. JupiterBestPracticesInstall it once from Recipe actions, then run it on every repository under the current directory. New to the Moderne CLI? Set it up.
Recipe list
It runs these recipes, in this order.
- Migrate Hamcrest assertions to JUnit Jupiter
- Statically import JUnit Jupiter assertions
- Remove JUnit 5 static Assertions imports in Kotlin when wildcard import is present
- Clean up assertions
- Replace
@CsvSourcewith@ValueSourcefor single method arguments - Remove unnecessary
assertEqualsdelta argument for integral types - Replace JUnit
assertTrue(false)andassertFalse(true)withfail() - Remove JUnit
assertTrue(true)andassertFalse(false) - Kotlin test methods should have return type
Unit - Remove
testprefix from JUnit 5 tests - Simplify
throwsstatements of tests - Remove
publicvisibility of JUnit 5 tests - Test methods should have void return type
- Add missing
@ParameterizedTestannotation when@ValueSourceis used or replace@Testwith@ParameterizedTest - Remove duplicate uses of
@TestTemplateimplementations for a single method - Replace
fail()intry-catchblocks withAssertions.assertDoesNotThrow(() -> { ... }) - Make lifecycle methods non-private
- Applies JUnit 5
assertThrowson last statement in lambda block only assertTrue(x instanceof y)toassertInstanceOf(y.class, x)- Make implausibly long
@Timeoutvalues explicit in minutes - Use JUnit5's
assertSameorassertNotSameinstead ofassertTrue(... == ...) - Remove JUnit Jupiter migrationsupport