JUnit Migration Guide from JUnit 4 to JUnit 5

Wenqi Glantz
2 min readSep 6, 2021
Photo by Wenqi at PA Middle Creek, snow geese migration

JUnit 5 is a modular and modern take on the JUnit 4 framework. Given its many advantages over JUnit 4, I recommend using JUnit 5 for writing unit tests in your microservices. This page serves as the guideline for migrating from JUnit 4 to JUnit 5 for Spring Boot microservices.

Limitations of JUnit 4:

  • The entire framework was contained in a single jar library. The whole library needs to be imported even when only a particular feature is required. In JUnit 5, we get more granularity and can import only what is necessary.
  • One test runner can only execute tests in JUnit 4 at a time (e.g. SpringJUnit4ClassRunner or Parameterized ). JUnit 5 allows multiple runners to work simultaneously.
  • JUnit 4 never advanced beyond Java 7, missing out on a lot of features from Java 8. JUnit 5 makes good use of Java 8 features.

JUnit migration steps:

1. Add JUnit 5 pom dependencies

See pom snippet below, junit-jupiter.version refers to version 5.7.2, or newer version when it becomes available per maven repository. Remember to remove junit4 dependency.

--

--