为测试正确配置 JUNIT5

Configure JUNIT5 correctly for Tests

我的项目几乎没有测试,当我执行整个测试时 class 我得到 No tests were found 但是当我独立测试每个方法时它们仍然有效并且我发现 JUnit5 配置不正确. 以下是我的jar和pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>...</name>
    <description>...</description>

    <properties>
        <java.version>8</java.version>
        <junit-platform.version>5.6.2</junit-platform.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.2.32</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.4.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.el</groupId>
            <artifactId>javax.el-api</artifactId>
            <version>2.2.4</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.3.Final</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <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>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit-platform.version}</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-platform.version}</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import static org.junit.jupiter.api.Assertions.assertThrows;

@SpringBootTest
public class ApplicationTests {

    private RestTemplate restTemplate = new RestTemplate();

    private static String BASE_PATH = "http://localhost:8080/exams/";

    @Test
    public void testNotFoundStartExamException() {
        final String url = BASE_PATH + "start/" + null + "/";

        assertThrows(HttpClientErrorException.class, () -> restTemplate.getForEntity(url, String.class));
    }

    @Test
    public void testNotFoundFinishExamException() {
        final String url = BASE_PATH + "finish-page/" + null + "/";

        assertThrows(HttpClientErrorException.NotFound.class, () -> restTemplate.getForEntity(url, String.class));
    }
}

当我使用命令mvn test时,它给出了以下结果

[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.137 s
[INFO] Finished at: 2020-05-19T08:18:07+03:00
[INFO] ------------------------------------------------------------------------

根据我需要的文档,我不确定要使用什么,为什么它不起作用以及如何让它起作用 jupiter-engine, platform-launcher, vintage-engine。我做错了什么?

问题是由于 duplicate/multiple junit 依赖项被添加到 pom.xml。如前所述,您有 spring-boot-starter-test,它已经包含了 junit Jupiter(5.5.2 for 2.2.6.RELEASE spring starter)对您的依赖。您已手动添加更高版本的 junit (5.6.2)。

如果你看一下 Junit 5 User Guide 它指定 junit-jupiter-engine in test runtime scope 但由于你没有指定任何范围它会进入编译范围,你可以通过检查 Effective Pom 来验证它。

简单的修复方法是删除手动添加的 junit 依赖项和 运行 mvn clean test。你的测试应该被选中