DEV Community

Artem Ptushkin
Artem Ptushkin

Posted on

6

How to properly ignore Junit 4 in Gradle and Maven

Jump to the best example here for Gradle and Maven

Problem

Quite a lot of Java dependencies bring Junit 4 or Junit vintage engine as a transitive dependency.

As well as Junit vintage engine is designed to integrate 4 and 5 version, it allows to use both at the same time. When you want to finally migrate, it leads to problems such as:

  • Runtime issues: a junit4 class not found
  • A test has run, but no coverage aggregated
  • Not concise annotations usage

Solution

All these problems have only one proper solution:

Use one particular version of Junit to prevent misleading and misusage

Here I focus on the latest - Junit 5 version usage, hence, excluding of Junit 4.

One of the options is to ignore this dependency from a specific dependency:

testImplementation('org.springframework.boot:spring-boot-starter-test') {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'  
}
Enter fullscreen mode Exit fullscreen mode
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
      <exclusion>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
Enter fullscreen mode Exit fullscreen mode

But, it requires you to know which dependency brings unpleasant dependency for you. In this case, you can use this command to track:

./gradlew dependencyInsight --dependency "junit:junit" --configuration "testCompileClasspath"
Enter fullscreen mode Exit fullscreen mode

Another option is to exclude not necessary dependencies from all the configurations. It leads you another problems such as: some library still requires Junit 4 / vintage. But, the compilation / runtime reflection errors actually only makes these dependencies visual and help to mark explicitly those Java modules, that still require on any reason to work with a legacy library.

Gradle:

Having configurations in Gradle it is easy to exclude a dependency in one particular place:

/* for all the configurations */
configurations {
  all {
    /* only junit 5 should be used */
    exclude group: 'junit', module: 'junit'
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
}
Enter fullscreen mode Exit fullscreen mode
/* for a particular test module */
configurations {
  testCompile {
  /* only junit 5 should be used */
    exclude group: 'junit', module: 'junit'
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
  testRuntime {
    /* only junit 5 should be used */
    exclude group: 'junit', module: 'junit'
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
}
Enter fullscreen mode Exit fullscreen mode

And don't forget to leave a comment for the future!

Resume

Hope this note helped to ignore unpleased dependency and keep control over your build configuration.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay