'Y'

JUnit 5 – new approach, new functionalities. Part 2

In the second part of our article on JUnit 5 – new approach, new functionalities we look at how to make the transition to JUunit 5. Happy reading.

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:

dependencies_junit5_java5.png

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:

simulated tests junit5.jpg

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.

Interested in JUnit? Check out our trainings.

Catalin Tudose
Java and Web Technologies Expert

Share the knowledge

Luxoft Warsaw - Warsaw Spire, plac Europejski 1, 00-844 Warszawa
Dimitrie Pompeiu nr 5-7 , building C, Et. 5, sect 2, Bucharest, 014459

Contact phone:

021 371 4858
Luxoft Poland Wroclaw - Silver Tower pl. Konstytucji 3-go Maja 3 50-048 Wroclaw
Aleja Generała Tadeusza Bora-Komorowskiego 25, Quattro Business Park Five, 31-476 Kraków, Poland

Contact phone:

+48 122110650
Success
Thank you.
Your request has been received.