JUnit 5 – new approach, new functionalities. Part 2
The step towards JUnit 5
In order to use Junit 5 in a Java project, the following dependencies must be added to the Maven configuration:
Below are the most important differences between Junit 4 and Junit 5:
Architecture
JUnit 4 | JUnit 5 |
Monolithic |
Junit Platform Junit Jupiter Junit Vintage |
Java versions necessary for running
JUnit 4 | JUnit 5 |
At least Java 5 | At least Java 8 |
Annotations
JUnit 4 | JUnit 5 |
@BeforeClass, @AfterClass
@Before, @After
@Ignore @Category |
@BeforeAll, @AfterAll
@BeforeEach, @AfterEach
@Disable @Tag |
First Junit 5 test
Unlike Junit 4, the test class and test methods can be package private. A simulated test looks like this:
A few remarks about the test above:
- The method annotated with @BeforeAll will be executed once, before the tests are running.
- The method annotated with @BeforeEach will be executed every time, before a test is ran.
- The methods annotated with @Test will be executed one by one, in order to check functionality.
- The method annotated with @AfterEach will be executed every time after running a test.
- The method annotated with @AfterAll will be executed once, after running all the tests.