简体   繁体   中英

Configure JUNIT5 correctly for Tests

I have few tests on my project and when I execute the whole test class I get No tests were found but when I test each method independently they still work and I figured out JUnit5 is not configured correctly. The following are my jars and 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));
    }
}

and when I use command mvn test it gives the following result

[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] ------------------------------------------------------------------------

I am not sure what to use, why it does not work and how to make it work, according to documentation I need jupiter-engine, platform-launcher, vintage-engine . What am I doing wrong?

The issue is due to duplicate/multiple junit dependency being added to pom.xml. As mentioned earlier you have spring-boot-starter-test which already includes junit Jupiter ( 5.5.2 for 2.2.6.RELEASE spring starter) dependency for you. You have manually added higher version of the junit (5.6.2).

在此处输入图像描述

If you take a look at the Junit 5 User Guide it specifies junit-jupiter-engine in test runtime scope but since you have not specified any scope it goes to compile scope which you can verify it from checking the Effective Pom.

Simple way to fix is to remove the manually added junit dependency and run mvn clean test . your test should get picked

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM