依赖项中的JUnit版本错误

时间:2019-07-16 10:47:38

标签: spring maven spring-boot junit junit5

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

mvn依赖项的相关部分:树

[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来自哪里?

1 个答案:

答案 0 :(得分:2)

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

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

这将控制spring boot 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

中定义的版本
相关问题