JUnit 5 usage and first test

How to use JUnit 5 in a Java project, differences between JUnit 4 and JUnit 5 and testing with JUnit5.

4. Making the step to JUnit 5

In order to be able to use JUnit 5 in a Java project, the following dependencies must be added to the Maven configuration:

Making the step to JUnit 5.png

The following differences are to be observed between JUnit 4 and JUnit 5:

JUnit 4_JUnit 5_architecture.png

supported_java_version.png

annotations_junit4_junit5.png

assertions_junit4_junit5.png

assumptions_junit4_junit5.png


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:

First JUnit 5 test.JPG


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.

Catalin Tudose
Java and Web Technologies Expert

Share the knowledge

Still have questions?
Connect with us
Thank you.
Your request has been received.
Thank you!
The form has been submitted successfully.