JUnit Jupiter
Best practices for JUnit Jupiter tests.
Composite Recipes
Recipes that include further recipes, often including the individual recipes below.
- Clean Up Assertions
- Exclude JUnit 4, unless Spring Boot Testcontainers is used
- JUnit 5 best practices
- JUnit Jupiter migration from JUnit 4.x
- Upgrade Surefire Plugin
- Upgrade to JUnit 5.14
- Use
Assertions#assume*(..)and Hamcrest'sMatcherAssume#assume*(..) - Use
MatcherAssert#assertThat(..) - Use Mockito JUnit Jupiter extension
- Use OkHttp 3 MockWebServer for JUnit 5
- Use Vert.x JUnit 5 Extension
Recipes
- Add Hamcrest JUnit dependency
- Add JUnit Jupiter dependencies
- Add missing
@BeforeEach,@AfterEach,@Testto overriding methods - Add missing
@ParameterizedTestannotation when@ValueSourceis used or replace@Testwith@ParameterizedTest - Applies JUnit 5
assertThrowson last statement in lambda block only - Cleanup JUnit imports
- Gradle
Testuse JUnit Jupiter - JUnit 4
@Categoryto JUnit Jupiter@Tag - JUnit 4
@Rule Timeoutto JUnit Jupiter'sTimeout - JUnit 4
@RunWithto JUnit Jupiter@ExtendWith - JUnit 4
@RunWith(Enclosed.class)to JUnit Jupiter@Nested - JUnit 4
@RunWith(Parameterized.class)to JUnit Jupiter parameterized tests - JUnit 4
AssertTo JUnit JupiterAssertions - JUnit 4
ExpectedExceptionTo JUnit Jupiter'sassertThrows() - JUnit 4
MockitoJUnitto JUnit JupiterMockitoExtension - JUnit 5 inner test classes should be annotated with
@Nested - JUnit TestName @Rule to JUnit Jupiter TestInfo
- Make
@TempDirfields non final - Make lifecycle methods non private
- Migrate JUnit 4
@Testannotations to JUnit 5 - Migrate JUnit 4
TestCaseto JUnit Jupiter - Migrate JUnit 4 environmentVariables rule to JUnit 5 system stubs extension
- Migrate JUnit 4 lifecycle annotations to JUnit Jupiter
- Migrate from JUnit 4
@FixedMethodOrderto JUnit 5@TestMethodOrder - OkHttp 3.x
MockWebServer@RuleTo 4.xMockWebServer - Pragmatists
@RunWith(JUnitParamsRunner.class)to JUnit Jupiter@Parameterizedtests - Remove JUnit 4
@RunWithannotations that do not require an@ExtendsWithreplacement - Remove duplicates uses of @TestTemplate implementations for a single method
- Replace
@CsvSourcewith@ValueSourcefor single method arguments - Replace
fail()intry-catchblocks withAssertions.assertDoesNotThrow(() -> { ... }) - Statically import JUnit Jupiter assertions
- Use JUnit Jupiter
@Disabled - Use JUnit Jupiter
@TempDir - Use JUnit Jupiter
Executable - Use JUnit5's
assertSameorassertNotSameinstead ofassertTrue(... == ...) - Use XMLUnit Legacy for JUnit 5
- Use wiremock extension
- assertTrue(x instanceof y) to assertInstanceOf(y.class, x)