Practical Guide to Migrate from JUnit 4 to JUnit5 — Part 1
A practical guide to migrate from JUnit 4 to JUnit 5.
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.
Practical Guide to JUnit 5 Parameterized Tests
Learn how to write JUnit 5 parameterized tests.
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
- It allows the use of parameterized tests easily by using the
- 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.
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.
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
timeout attribute. It has been replaced with
assertTimeout() methods respectively.
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).
And there we have it. I hope you have found this useful. Thank you for reading. If you enjoyed this article, remember to follow me for more updates!
Stay tuned for my next article, to talk about the Runners,Temporary Folder and Rules✌️.
If you are not a Medium member yet and want to become one, click here.
Join Medium with my referral link — Wei Kang
As a Medium member, a portion of your membership fee goes to writers you read, and you get full access to every story…