Published in


Practical Guide to Migrate from JUnit 4 to JUnit5 — Part 1

A practical guide to migrate from JUnit 4 to JUnit 5.

Photo by Green Chameleon on Unsplash

In this article, I will be sharing some steps to help you migrate your JUnit 4 tests to JUnit 5 tests. You may also visit the link below to learn how to use the JUnit 5 parameterized tests, which I have previously written.

What are the Benefits of JUnit 5?

Before that, let’s look at some benefits of JUnit 5.

  • It is more granular and you can import only what is necessary for your project. It is composed of 3 modules: Platform, Jupiter, and Vintage. The platform serves as a foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform. The Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. The Vintage provides a TestEngine for running JUnit 4 tests with JUnit 5. Therefore, with the Vintage, you can plan your migration plan to migrate your tests gradually without the need to do a big bang change.
  • It allows the use of multiple extensions at a time, while JUnit 4 could only allow 1 test runner at a time.
  • It allows the use of nested tests easily by using the @Nested annotation.
  • It allows the use of parameterized tests easily by using the @ParameterizedTest annotation.
  • It leverages Java 8 or later features (e.g. lambdas) which makes tests more powerful and easier to maintain.
  • It has new features for describing and organizing your tests (e.g. @Tag and @DisplayName)

1. Dependencies

You will need these new dependencies below to run the tests. If you do not need to run any JUnit 4 tests, you may exclude the Vintage engine dependency. Otherwise, the Jupiter dependency is sufficient for us to write and run JUnit 5 tests.

2. Annotations

You will notice a few changes to the annotations we use for JUnit 5 compared to JUnit 4. First, the annotations are now in the org.junit.jupiter.api package instead of the org.junit package. Second, below are the difference between the JUnit 4 and JUnit 5 annotations.

From the list above, we should be able to find a replacement for the JUnit 4 annotations with the JUnit 5.

You may also notice that for JUnit 5, there is no more expected or timeout attribute. It has been replaced with assertThrows() and assertTimeout() methods respectively.

JUnit 4 Example
JUnit 5 Example

You may also noticed that in my JUnit 5 example above, the test classes and test methods are not required to be public. According to the JUnit documentation, it is generally recommended to omit the public modifier for test classes, test methods, and lifecycle methods unless there is a technical reason for doing so (for example, when a test class is extended by a test class in another package).

Sample from SonarLint



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Wei Kang

Wei Kang

Once a programmer, always a programmer