JUnit Jupiter best practices

MSAL

Applies 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.JupiterBestPractices
mod run . --recipe=org.openrewrite.java.testing.junit.JupiterBestPractices

Install 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.

  1. Migrate Hamcrest assertions to JUnit Jupiter
  2. Statically import JUnit Jupiter assertions
  3. Remove JUnit 5 static Assertions imports in Kotlin when wildcard import is present
  4. Clean up assertions
  5. Replace @CsvSource with @ValueSource for single method arguments
  6. Remove unnecessary assertEquals delta argument for integral types
  7. Replace JUnit assertTrue(false) and assertFalse(true) with fail()
  8. Remove JUnit assertTrue(true) and assertFalse(false)
  9. Kotlin test methods should have return type Unit
  10. Remove test prefix from JUnit 5 tests
  11. Simplify throws statements of tests
  12. Remove public visibility of JUnit 5 tests
  13. Test methods should have void return type
  14. Add missing @ParameterizedTest annotation when @ValueSource is used or replace @Test with @ParameterizedTest
  15. Remove duplicate uses of @TestTemplate implementations for a single method
  16. Replace fail() in try-catch blocks with Assertions.assertDoesNotThrow(() -> { ... })
  17. Make lifecycle methods non-private
  18. Applies JUnit 5 assertThrows on last statement in lambda block only
  19. assertTrue(x instanceof y) to assertInstanceOf(y.class, x)
  20. Make implausibly long @Timeout values explicit in minutes
  21. Use JUnit5's assertSame or assertNotSame instead of assertTrue(... == ...)
  22. Remove JUnit Jupiter migrationsupport