How to use JUnit 5.2 BOM in Maven dependency?
Asked Answered
M

2

10

According to the newly released v. 5.2 of JUnit, there is now a BOM:

JUnit BOM: To ease dependency management using Maven or Gradle, a Bill of Materials POM is now provided under the org.junit:junit-bom:5.2.0 Maven coordinates.

As a starting point, currently my POM looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.bosspanda.tmp</groupId>
    <version>0.1-SNAPSHOT</version>

    <artifactId>tmp</artifactId>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.source>10</maven.compiler.source>
        <maven.compiler.target>10</maven.compiler.target>
        <java.version>10</java.version>
        <junit.version>4.12</junit.version>
        <junit.jupiter.version>5.2.0</junit.jupiter.version>
        <junit.vintage.version>5.2.0</junit.vintage.version>
        <junit.platform.version>1.2.0</junit.platform.version>
    </properties>
        <dependencies>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-params</artifactId>
                <version>${junit.jupiter.version}</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
                <version>${junit.vintage.version}</version>
                <scope>test</scope>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
</project>

If I understand this BOM release note above correctly, it is a simplification of this junit <dependency> tags into a single BOM dependency.

However, I have stark troubles integrating it into my project's pom.xml. After having a look at the linked resource (https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Importing_Dependencies) I came to the conclusion that I have to replace the distinct dependencies with a single:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>5.2.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

But with this in place IntelliJ IDEA (v. 2018.1.3 Ultimate x64, Maven v. 3.5.3, target JDK 10.0.1) does not seem to know what the <dependencyManagement> tag is and does not parse its content. No modules are registered in the project structure.
But also if I remove the <dependencyManagement> tag, the BOM does not get loaded by IDEA.
I also tried adding the maven coordinates of the BOM via IDEA by using the maven plugin, but while it finds the distinct junit packages, it does not find the BOM and cannot download anything from org.junit:junit-bom:5.2.0

How do I add this BOM file to my pom dependency?
Thank you!

Maribeth answered 8/5, 2018 at 20:16 Comment(2)
The answer from Jens Piegsa is correct, and you can find an example here: github.com/junit-team/junit5-samples/commit/…Affine
@SamBrannen thanks for the link, this helped a lot!Maribeth
R
13

Referencing a bom file under <dependencyManagement><dependencies> only manages versions to be compatible. You still need to declare all needed dependencies under <dependencies> but without <version>. Thats how Maven bom references work. IntelliJ can handle them that way too.

Reyesreykjavik answered 8/5, 2018 at 23:11 Comment(1)
I see. Thank you very much for the clarification. The introduction page by maven was a bit misleading about this.Maribeth
C
8

The following worked for me using surefire.plugin.version 2.22.2

junit-jupiter artifact brings in all other necessary artifacts, individual direct dependencies are not needed in this case.

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.junit</groupId>
      <artifactId>junit-bom</artifactId>
      <version>5.5.2</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
  </dependency>
  <!--Optional: Supports running Junit4 along with Junit5 -->
  <dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
Celibacy answered 29/10, 2019 at 0:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.