我已经通过Spring Initilizr创建了一个Spring Boot 2.2.6应用程序,其中包括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>
答案 0 :(得分:0)
以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,引擎和参数化测试组件。
顺便说一句,如果要使用Spring Boot更改JUnit的版本,则必须将其添加到您的文件中:
<junit-jupiter.version>5.6.0</junit-jupiter.version>
答案 1 :(得分:-1)
您正在同时使用JUnit4(Assert
)和JUnit 5(@Test
)。最初,Maven将尝试确定运行测试所需的提供程序,并以某种方式选择了JUnit4。
编辑测试以使用JUnit5(又称Juipiter)Assertions API。将org.junit.Assert.assertEquals(...)
更改为org.junit.jupiter.api.Assertions.assertEquals(....)
不要忘记在所有测试文件中将org.junit.Assert.*
的发生次数更改为org.junit.jupiter.api.Assertions.*
。