如何在Spring Test Suite中运行JUnit 5测试?

时间:2019-01-10 21:06:21

标签: spring-boot junit

STS:3.9.7。发布

SpringBoot项目:2.1.1。发布

有效的POM显示<junit.version>4.12</junit.version>。我希望使用JUnit 5,SpringBoot添加了JUnit 4依赖关系也让我有些惊讶。我应该手动将JUnit 5依赖项添加到POM中还是最好不要弄乱它?

2 个答案:

答案 0 :(得分:0)

是的。默认情况下,您必须手动将JUnit5添加为spring-boot-starter-test,仅包括JUnit4。

首先,从spring-boot-starter-test中排除JUnit4:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <!-- Exclude JUnit 4 -->
            <exclusion>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
            </exclusion>
        </exclusions>
</dependency>

然后包含JUnit5依赖项:

<!-- Actual Junit5 implementation. Will transitively include junit-jupiter-api -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <scope>test</scope>
</dependency>

<!-- For junit5 parameterised test support -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <scope>test</scope>
</dependency>

<!-- Only required to run junit5 test from IDE -->
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <scope>test</scope>
</dependency>

答案 1 :(得分:0)

还应考虑排除:groupId:org.junit.vintage,artifactId:junit-vintage-engine,它也出现在spring-boot-starter-test的更高版本中,并且还包含JUnit4类。

相关问题