JUnit 5 – usage and first test
In order to be able to use JUnit 5 in a Java project, the following dependencies must be added to the Maven configuration:
The following differences are to be observed between JUnit 4 and JUnit 5:
5. First JUnit 5 test
Unlike in JUnit 4, the test class and the test methods can be package private. This is how a simple test will look like:
Remarks on the test above:
- The method annotated with @BeforeAll will be executed once, before running the tests.
- The method annotated with @BeforeEach will be executed each time before running each test.
- The method annotated with @Test will be executed in order to verify the functionality.
- The method annotated with @AfterEach will be executed each time after running each test.
- The method annotated with @AfterAll will be executed once, after running all tests.
Interested in learning how to work with this technology? Check out our JUnit Testing training.
Java and Web Technologies Expert