java - 使用 Spring Boot 和 JUnit5 在 Intellij 中终止所有测试
问题描述
我试图在我的 Spring Boot 应用程序中使用 JUnit5 编写一个简单的测试,我注意到我的所有测试都会立即终止。我下载了一些单元测试示例,它工作得很好,所以我不知道我的设置有什么问题。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
}
这是我终止的示例测试:
import org.junit.jupiter.api.Test;
class DemoClassTest {
@Test
void testOne() {
}
}
这是我的 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.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringDemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
解决方案
您应该删除junit-jupiter-engine
和mockito-junit-jupiter
依赖项,因为它已经包含在spring-boot-starter-test
其中,让 SpringBoot 为您处理依赖项之间的不兼容版本问题。
您可能还想删除junit-vintage-engine
排除项以允许 Junit5 和 Junit4 测试执行
推荐阅读
- python - Match only a character between two others
- c++ - Can an anonymous lambda function be bound to an object to allow the code in the lambda to access the object's members?
- google-chrome - Chrome ignores Nginx upstreams (loads only first)
- java - Why Spring JdbcTemplate is thread-safe?
- c# - Validate user input using C# RegularExpressions
- c# - Calling a WebAPI in .net framework from a console application in .net core
- angularjs - 如何处理来自控制器的下载返回文件结果?MVC
- python - Django 分页器页面返回 NoneType
- jmeter - 使用 Jmeter,如何启动 Amazon S3 以验证文件是否存在并计算文件数?
- ansible - Ansible:处理程序调用后清除主机错误