Migrating from JUnit 4 to JUnit 5: implementing the migration, dependencies, annotations. Part 2

Migrating from JUnit 4 to JUnit 5: implementing the migration, dependencies, annotations. Part 2

The second article of our series on implementing the migration, dependencies, annotations when Migrating from JUnit 4 to JUnit 5. This time we look at needed dependencies.
The second article of our series on implementing the migration, dependencies, annotations when Migrating from JUnit 4 to JUnit 5. This time we look at needed dependencies.

2. Needed dependencies


We discuss the migration process from JUnit 4 to JUnit 5. JUnit 5 lets us label tests with display names using its nested tests and dynamic tests.

JUnit 4 has a monolithic architecture, so there is a single dependency in the Maven configuration that supports running JUnit 4 tests (listing 1).

Listing 1 JUnit 4 Maven dependency

JUnit 4 Maven dependency.JPG

One JUnit 5 dependency, JUnit Vintage, can replace the dependency from listing 1 during migration. The first things to do in the migration process are at the level of the dependencies that are used.

The first dependency is junit-vintage-engine (listing 2). It belongs to JUnit 5 but ensures backward compatibility with previous versions of JUnit. Working with JUnit 5 Vintage, Maven transitively accesses the dependency to JUnit 4. Introducing this dependency is a first step in the migration JUnit 4 and JUnit 5 tests can coexist within the same project until the migration process is finalized.

Listing 2 JUnit Vintage Maven dependency

JUnit Vintage Maven dependency.JPG

Running the JUnit 4 tests now, we can see that they are successfully executed, as shown in figure 1. Working with the JUnit 5 Vintage dependency instead of the old JUnit 4 dependency will not make any difference.

Running JUnit 4 tests.png

Fig. 1 Running the JUnit 4 tests after replacing the old JUnit 4 dependency with JUnit 5 Vintage

After introducing the JUnit Vintage dependency, the migration path can continue with the introduction of JUnit 5 Jupiter annotations and features. The required dependencies are shown in the following listing.

Listing 3 The most useful JUnit Jupiter Maven dependencies

useful JUnit Jupiter Maven dependencies.JPG

To write tests using JUnit 5, we will always need the junit-jupiter-api and junit-jupiter-engine dependencies. The first represents the API for writing tests with JUnit Jupiter (including the annotations, classes, and methods to be migrated to). The second represents the core JUnit Jupiter package for the execution test engine.

An additional dependency that we may need is junit-jupiter-params (for running parameterized tests). At the end of the migration process (when no more JUnit 4 tests are left), we can remove the first junit-vintage-engine dependency, presented in listing 2.

Interested in JUnit? Check out our trainings.


Catalin Tudose
Java and Web Technologies Expert
Nadal masz pytania?
Połącz sięz nami