依赖项中的 JUnit 版本错误

Wrong version of JUnit in dependencies

我想 运行 在我的 Spring 引导应用程序上测试 JUnit 5.4+,这样我就可以在我的测试中使用 @Order 注释。但是,无论我尝试什么,Maven 都会将我的 POM 解析为 5.3.2。 我已经尝试包括我可以手动想到的所有依赖项,但最终我得到了一堆不匹配的版本。我还尝试清除整个 ~/.m2/repository 文件夹并重建树,结果相同。

mvn相关部分dependency:tree

[INFO] +- org.junit.jupiter:junit-jupiter-api:jar:5.5.0:test
[INFO] |  +- org.apiguardian:apiguardian-api:jar:1.1.0:test
[INFO] |  +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] |  \- org.junit.platform:junit-platform-commons:jar:1.3.2:test
[INFO] +- org.junit.jupiter:junit-jupiter:jar:5.5.0:test
[INFO] |  +- org.junit.jupiter:junit-jupiter-params:jar:5.3.2:test
[INFO] |  \- org.junit.jupiter:junit-jupiter-engine:jar:5.3.2:test
[INFO] |     \- org.junit.platform:junit-platform-engine:jar:1.3.2:test

部分pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.5.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.5.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
...
<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
...

5.3.2 是哪里来的?

将此行添加到 Maven 中的属性 pom.xml:

<junit-jupiter.version>5.5.0</junit-jupiter.version>

这将控制 spring 引导 poms (org.springframework.boot:spring-boot-dependencies) 中依赖管理中定义的依赖。


原因是:org.springframework.boot:spring-boot-dependencies包括junit-bom

  <dependency>
    <groupId>org.junit</groupId>
    <artifactId>junit-bom</artifactId>
    <version>${junit-jupiter.version}</version>
    <type>pom</type>
    <scope>import</scope>
  </dependency>

默认 junit-jupiter.version5.3.2。因此,只要您不更改 junit-jupiter.version,此 bom 将定义所有未明确列出的依赖项(例如 org.junit.jupiter:junit-jupiter-params)都是 org.junit:junit-bom:5.3.2

中定义的版本