The JUnit 5 Testing Framework (TFW) is composed of a custom runner and reporter implementation (as defined by the TEE API), and also contains a subset of the standard JUnit 5 modules. They are contained in the JUnit 5 TFW bundle – com.prosyst.mbs.tee.tfw.junit5.jar.
The JUnit 5 itself consists of the JUnit Platform, JUnit Jupiter, and helper libraries –opentest4j and apiguardian:
JUnit Platform is the base layer that allows launching different testing models. It defines the TestEngine API.
JUnit Jupiter combines a new programming and extension model for writing tests and extensions that run on top of the JUnit 5 Platform.
A JUnit Vintage module also exists, which is not included in TEE since TEE has separate Testing Frameworks for running older versions of JUnit.
At the time of writing the JUnit 5 TFW there are no other testing models other than JUnit Jupiter.
Key Features
JUnit 5 allows:
Writing Dynamic tests. The structure of a dynamic test is not known before the test execution, i.e. a test method that is marked as dynamic (using @TestFactory annotation) can generate random tests tree.
Writing Parameterized tests using @ParameterizedTest annotation, i.e. a test method can be run one or multiple times with different parameter values.
Writing Repeated Tests using @RepeatedTest annotation.
Providing custom Display Names.
Extending the testing model. JUnit 5 provides a flexible extension mechanism.
Requirements
To run JUnit 5 Testing Framework, Java 8 or above is required. However, it can still test code that has been compiled with previous versions of the JDK (Java 5 or above).
Java Annotations
The java core annotations, that can be used in tests are defined in org.junit.jupiter.api package:
@Test
@ParameterizedTest
@RepeatedTest
@TestFactory
@TestInstance
@TestTemplate
@DisplayName
@BeforeEach
@AfterEach
@BeforeAll
@AfterAll
@Nested
@Tag
@Disabled
@ExtendWith
More information about the annotations and a thorough guide on writing JUnit 5 tests can be found in the JUnit 5 documentation.
Differences with Previous Versions of JUnit and Useful Tips
Neither test classes nor test methods need to be public in JUnit 5.
Unlike previous JUnit versions, in JUnit 5 the assert methods (refer to org.junit.jupiter.api.Assertions class) have the assertion message as the last argument of the method. This could cause hard-to-find issues when writing tests. For example, calling the assertNotNull method:
public static void assertNotNull(Object actual, String message)
like this
assertNotNull("test message", val);
will always pass, which is not intended, obviously.
The methods annotated with @BeforeEach/@AfterEach are executed before/after any test methods in the class.
The methods annotated with @BeforeAll/@AfterAll are executed only once before/after any test methods in the class. They should be static unless the test class is marked with @TestInstance(Lifecycle.PER_CLASS) annotation.
In JUnit 5 there is no test suite. Instead, nested classes could be used.
Only non-static nested classes (i.e. inner classes) can serve as @Nested test classes.
Test classes and test methods can declare custom display names (incl. spaces and special characters) that will be shown in the reports.