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.

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more