Skip to main content
blog title image

1 minute read - Java For Testers JUnit Java Maven JUnit Examples

Junit 4 vs 5 - using both in same project

Jun 23, 2020

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

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