Junit 5 не может найти тесты (Spring Boot) - PullRequest
1 голос
/ 15 апреля 2020

Я создал приложение Spring Boot 2.2.6 через Spring Initilizr, которое включает JUnit 5.6. Я использую сгенерированный pom. xml вместе с некоторыми дополнительными зависимостями и Intellij IDEA 2020.1 в качестве моей IDE.

Я создал очень простой тест, чтобы посмотреть, работают ли тесты:

package com.hua.geoutils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class GeoUtilsTest
{

    @Test
    public void testSplitBoundingBox()
    {
        Assertions.assertEquals(1,1);
    }
}

Я выполняю mvn test spring-boot:run и на жизненном цикле теста получаю:

[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ atlantis-rest ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

Кажется, что maven даже не обнаруживает, что тесты существуют, и я не могу понять, почему. Любые идеи о том, что здесь происходит не так?

РЕДАКТИРОВАТЬ:

Вот мой 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>com.hua</groupId>
    <artifactId>atlantis-rest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>atlantis-rest</name>
    <description>Atlantis REST Web Service</description>
    <!-- <packaging>war</packaging> -->

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.11</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>3.4.2</version>
            <!-- <type>bundle</type> -->
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.recombee/api-client -->
        <dependency>
            <groupId>com.recombee</groupId>
            <artifactId>api-client</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </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.springframework.restdocs</groupId>
            <artifactId>spring-restdocs-mockmvc</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.asciidoctor</groupId>
                <artifactId>asciidoctor-maven-plugin</artifactId>
                <version>1.5.8</version>
                <executions>
                    <execution>
                        <id>generate-docs</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>process-asciidoc</goal>
                        </goals>
                        <configuration>
                            <backend>html</backend>
                            <doctype>book</doctype>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.restdocs</groupId>
                        <artifactId>spring-restdocs-asciidoctor</artifactId>
                        <version>${spring-restdocs.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- <configuration>
                    <executable>true</executable>
                </configuration> -->
            </plugin>
        </plugins>
    </build>
</project>

Ответы [ 2 ]

0 голосов
/ 15 апреля 2020

Принимая https://info.michael-simons.eu/2018/06/18/maven-use-junit-5-with-spring-boot-for-unit-and-integration-tests/ в качестве отправной точки, я рекомендую добавить движок Юпитера в качестве явной зависимости:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <scope>test</scope>
</dependency>

В качестве альтернативы, вы можете включить junit-jupiter, который является агрегатный компонент вместо junit-jupiter-api. junit-jupiter содержит API, движок и компонент параметризованных тестов.

Кстати, если вы хотите изменить версию JUnit с пружинной загрузкой, вы должны добавить это к своим свойствам:

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

Подробнее см. { ссылка }

0 голосов
/ 15 апреля 2020

Вы используете JUnit4 (Assert) и JUnit 5 (@Test). Первоначально Maven будет пытаться решить, какой поставщик необходим для запуска тестов, и каким-то образом был выбран JUnit4.

Редактирование тестов для использования API утверждений JUnit5 (также известный как Juipiter). Измените org.junit.Assert.assertEquals(...) на org.junit.jupiter.api.Assertions.assertEquals(....)

И не забудьте изменить org.junit.Assert.* вхождений на org.junit.jupiter.api.Assertions.* во всех тестовых файлах.

...