junit_5
Table of Contents
JUnit 5
- Java 8 necessary
Most used annotations
@Test@BeforeEach@BeforeAll@AfterEach@AfterAll@Disabled
- Test Method:
- any instance method that is directly annotated or meta-annotated with
@Test,@RepeatedTest,@ParameterizedTest,@TestFactory, or@TestTemplate. - not
private - not (more) required to be
public
Meta-Annotations / Composed Annotations
Multiple annotations can be composed to one, which can be used later instead.
Example 1
Replace @Tag(“fast”) with @Fast
@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Tag("fast") public @interface Fast { }
Usage:
@Fast @Test void myFastTest() { // ... }
Example 2
Combine @Tag(“fast”) and @Test to one @FastTest.
@Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Tag("fast") @Test public @interface FastTest { }
Usage:
@FastTest void myFastTest() { // ... }
Assertions
- Using lambdas
- Grouped assertions
- Dependent assertions
- Exception testing
- Timeout not exceeded
- (simply)
- with result
- with method
- Timeout exceeded
- (simply)
- with preemptive termination
- Kotlin assertions support
- Third party library support
- AssertJ
- Hamcrest
- Truth
Assumptions
In direct contrast to failed assertions, failed assumptions do not result in a test failure; rather, a failed assumption results in a test being aborted.
Assumptions are typically used whenever it does not make sense to continue execution of a given test method — for example, if the test depends on something that does not exist in the current runtime environment.
- assumeTrue()
- assumeFalse()
- assumingThat()
Conditional Test Execution
@DisabledOnOs@DisabledOnJre/@DisabledForJreRange@DisabledIfEnvironmentVariable/@DisabledIfEnvironmentVariables@DisabledIfSystemProperty/@DisabledIfSystemProperties@EnabledOnOs@EnabledOnJre/@EnabledForJreRange@EnabledIfEnvironmentVariable/@EnabledIfEnvironmentVariables@EnabledIfSystemProperty/@EnabledIfSystemProperties
Test Execution Order
@TestMethodOrder(OrderAnnotation.class) class OrderedTestsDemo { @Test @Order(1) void nullValues() { // perform assertions against null values } @Test @Order(2) void emptyValues() { // perform assertions against empty values } @Test @Order(3) void validValues() { // perform assertions against valid values } }
Test Instance Lifecycle
Parameterized Tests
@ParameterizedTest
Repeated Tests
- @RepeatedTest
Dynamic Tests
- @TestFactory
Templates for Test Cases
- @TestTemplate
Misc
@TestMethodOrder- defines test method execution order@TestInstance- ???@DisplayName- declares custom display name for test@DisplayNameGeneration- declares custom display name generator@Nested- ???@Tag- makes possible grouping of tests either on class or on method level@Timeout- used to fail tests, if their execution exceeds a given duration@ExtendWith- ???@RegisterExtension- ???@TempDir- ???
JUnit 5 Pros
- More granularity
- Multiple runners work simultaneously
- Java 8 language features
JUnit 4 -> JUnit 5
- @Before → @BeforeEach
- @After → @AfterEach
- @BeforeClass → @BeforeAll
- @AfterClass → @AfterAll
- @Ignore → @Disabled
junit_5.txt · Last modified: by kalaschnikow
