JUnit Jupiter
Best practices for JUnit Jupiter tests.
Composite Recipes
Recipes that include further recipes, often including the individual recipes below.
- Clean Up Assertions
- JUnit Jupiter best practices
- JUnit Jupiter migration from JUnit 4.x
- Upgrade Surefire Plugin
- 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
,@Test
to overriding methods - Add missing
@ParameterizedTest
annotation when@ValueSource
is used or replace@Test
with@ParameterizedTest
- Applies JUnit 5
assertThrows
on last statement in lambda block only - Cleanup JUnit imports
- Gradle
Test
use JUnit Jupiter - JUnit 4
@Category
to JUnit Jupiter@Tag
- JUnit 4
@RunWith
to JUnit Jupiter@ExtendWith
- JUnit 4
@RunWith(Enclosed.class)
to JUnit Jupiter@Nested
- JUnit 4
@RunWith(Parameterized.class)
to JUnit Jupiter parameterized tests - JUnit 4
Assert
To JUnit JupiterAssertions
- JUnit 4
ExpectedException
To JUnit Jupiter'sassertThrows()
- JUnit 4
MockitoJUnit
to JUnit JupiterMockitoExtension
- JUnit 5 inner test classes should be annotated with
@Nested
- JUnit TestName @Rule to JUnit Jupiter TestInfo
- Make
@TempDir
fields non final - Make lifecycle methods non private
- Migrate JUnit 4
@Test
annotations to JUnit 5 - Migrate JUnit 4
TestCase
to JUnit Jupiter - Migrate JUnit 4 lifecycle annotations to JUnit Jupiter
- Migrate from JUnit 4
@FixedMethodOrder
to JUnit 5@TestMethodOrder
- OkHttp 3.x
MockWebServer
@Rule
To 4.xMockWebServer
- Pragmatists @RunWith(JUnitParamsRunner.class) to JUnit Jupiter Parameterized Tests
- Remove JUnit 4
@RunWith
annotations that do not require an@ExtendsWith
replacement - Remove duplicates uses of @TestTemplate implementations for a single method
- Replace
fail()
intry-catch
blocks withAssertions.assertDoesNotThrow(() -> { ... })
- Statically import JUnit Jupiter assertions
- Use JUnit Jupiter
@Disabled
- Use JUnit Jupiter
@TempDir
- Use JUnit Jupiter
Executable
- Use JUnit5's
assertSame
orassertNotSame
instead ofassertTrue(... == ...)
- Use XMLUnit Legacy for JUnit 5
- Use wiremock extension
- assertTrue(x instanceof y) to assertInstanceOf(y.class, x)