# JUnit 5 测试 Spring 引擎的时候提示 junit-vintage 错误

**URL:** <https://www.isharkfly.com/t/junit-5-spring-junit-vintage/562>\
**Category:** 程序语言\
**Tags:** spring, java\
**Created:** [October 9, 2020, 3:07am UTC](https://www.isharkfly.com/t/junit-5-spring-junit-vintage/562 "2020-10-09T03:07:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [October 9, 2020, 3:07am UTC](https://www.isharkfly.com/t/junit-5-spring-junit-vintage/562/1 "2020-10-09T03:07:57Z")

</div>

在 Spring 项目中运行测试的时候，得到错误：

```auto
TestEngine with ID 'junit-vintage' failed to discover tests” with Spring

```

这个错误的原因是 JUnit 的引擎，使用了 junit-vintage 引擎。

junit-vintage 是 Junit 4 中使用的引擎，如果你的项目使用了 Junit 5 的话，你需要在 spring-boot-starter-test 中将 JUnit 4 的引擎从测试中删除。

```auto
        <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>

```

## junit-vintage-engine 和 junit-jupiter-engine 有什么不同

`junit-vintage-engine` 是 JUnit 4 中使用的测试引擎。  
`junit-jupiter-engine` 是 JUnit 5 中使用的测试引擎。

如果你的 Spring 项目使用的新的 Spring Boot 版本的话，你应该默认使用了 JUnit 5 的引擎，因此为了兼容性，你需要在 spring-boot-starter-test 这个 POM 引用的时候将 JUnit 4 的引擎去除掉。

 ![maven-spring-junit-test-01](https://cdn.isharkfly.com/com-isharkfly-www/discourse-uploads/original/3X/a/5/a51c75f4a5b3ec0ad11ad874c6f5b8c9b85537fb.png)

上面的这个配置你可以尝试下能解决你的问题。

---

<div class="post-metadata">

**Author:** ![lucas](https://www.isharkfly.com/user_avatar/www.isharkfly.com/lucas/32/20805_2.png) [@lucas](https://www.isharkfly.com/u/lucas)\
**Post date:** [January 28, 2021, 7:25pm UTC](https://www.isharkfly.com/t/junit-5-spring-junit-vintage/562/2 "2021-01-28T19:25:22Z")

</div>

在升级

```auto
spring-boot-starter-parent

```

版本到 2.4.0 以后，你不再需要下面的代码了，因为在 2.4.0 版本已经升级到 JUnit 5 了。

```auto
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>

```

如下图所示的代码，上面的代码将会在 Spring-boot 中使用 JUnit 5 的测试版本。

 ![spring-test-01](https://com-ossez-www-discourse.s3.dualstack.us-east-2.amazonaws.com/discourse-uploads/original/2X/f/f02c0f6359c7d19585a18d75658124bf9394ae0a.png)

完整的 Spring Boot 测试用例，你需要把下面的 Page 包含进来：

```auto
        <!-- TESTS -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <scope>test</scope>
        </dependency>

```
