Skip to main content

Jun 23, 2020 - 1 minute read - Java For Testers JUnit Java Maven JUnit Examples

Junit 4 vs 5 - using both in same project

I prefer to use either JUnit 4 or JUnit 5, but there may be moments when we want to use both. We can do that with JUnit 5 dependencies.

Rather than mixing the JUnit 4 and JUnit 5 dependencies, we might be able to get away with the JUnit 5 dependencies alone.

Explanation Video

Watch on YouTube

Comment out JUnit 4

It is easy to check. If your project can work with just the JUnit 5 dependency definitions.

        <!--
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        -->

Add in JUnit 5

There are two dependencies to add for JUnit 5

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>

And then a third for the ‘vintage’ dependency that allows us to use JUnit 4 annotations without changing our test code.

        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <version>5.6.2</version>
            <scope>test</scope>
        </dependency>

Make Sure Surefire Plugin has been added

And make sure that the Surefire plugin has been added to the project:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M4</version>
            </plugin>
        </plugins>
    </build>

Official Documentation

Source Example

You can find a project with a pom.xml that does this over here:

And the pom.xml


You will need a Github account to comment. Or you can contact me with your comment.

I reserve the right to delete spam comments e.g. if your comment adds no value and its purpose is simply to create a backlink to another site offering training, or courses, etc.