Gotcha: Running both JUnit4 and JUnit5 Together with Maven

Featured image for sharing metadata for article

About a month ago, and earlier today, I found an issue with partially upgrading my Java projects from JUnit4 to JUnit5. I was in a halfway house between upgrading all my tests to JUnit5, so still had some JUnit4 tests, but found that my Gradle build wasn't running the new JUnit5 tests.

It turns out that to use the new JUnit test runner, you need to explicitly set it up - it makes sense when you think about it!

My solution to getting this working with Maven was via the maven-surefire-plugin documentation for JUnit5, and adding the following to my pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M4</version> <!-- this version is important! -->
      <dependencies>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-engine</artifactId>
          <version>5.3.2</version>
        </dependency>
      </dependencies>
    </plugin>
  </plugins>
</build>

With this in place both my JUnit5 and legacy JUnit4 tests were running correctly!

Written by Jamie Tanna's profile image Jamie Tanna on , and last updated on .

Content for this article is shared under the terms of the Creative Commons Attribution Non Commercial Share Alike 4.0 International, and code is shared under the Apache License 2.0.

#blogumentation #java #maven #junit.

This post was filed under articles.

Interactions with this post

Interactions with this post

Below you can find the interactions that this page has had using WebMention.

Have you written a response to this post? Let me know the URL:

Do you not have a website set up with WebMention capabilities? You can use Comment Parade.