使用 Spring Boot 和 JUnit5 在 Intellij 中终止所有测试

     2023-02-27     222

关键词:

【中文标题】使用 Spring Boot 和 JUnit5 在 Intellij 中终止所有测试【英文标题】:All tests get terminated in Intellij with Spring Boot and JUnit5 【发布时间】:2020-09-19 21:45:29 【问题描述】:

我试图在我的 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>

Image from Intellij

【问题讨论】:

“被终止”是什么意思?您的测试中没有内容;它立即完成。 @chrylis-cautiouslyoptimistic- 无论测试方法的内容是什么,它都会以退出代码 -1 结束。 使用mvn testmvnw test(从根目录)运行它会发生什么 首先它说在我的环境中找不到 JAVA_HOME。修复此问题后,它说构建成功并且我运行了 0 次测试。 【参考方案1】:

您应该删除 junit-jupiter-enginemockito-junit-jupiter 依赖项,因为它已包含在 spring-boot-starter-test 中,这让 SpringBoot 为您处理依赖项之间的不兼容版本问题。

您可能还想删除 junit-vintage-engine 排除项以允许 Junit5 和 Junit4 测试执行

【讨论】:

我是不是需要使用 Kotlin Junit5 和 Spring Boot 将 Spring 注释复制到内部类?

】我是不是需要使用KotlinJunit5和SpringBoot将Spring注释复制到内部类?【英文标题】:ShouldIneedtocopySpringannotationstoinnerclasseswithKotlinJunit5andSpringBoot?我是否需要使用KotlinJunit5和SpringBoot将Spring注释复制到内部类?【发布时间】:2020-02-01... 查看详情

使用 spring boot gradle 插件无法使用依赖管理执行 junit5 测试

】使用springbootgradle插件无法使用依赖管理执行junit5测试【英文标题】:Failedtoexecutejunit5testswithdependencymanagementwithspringbootgradleplugin【发布时间】:2019-10-2622:46:05【问题描述】:在我基于gradle和kotlin的项目中,我有一些子模块。为i... 查看详情

使用 JUnit5+Spring Security 测试 Spring Boot 控制器

】使用JUnit5+SpringSecurity测试SpringBoot控制器【英文标题】:TestspringbootcontrollerswithJUnit5+SpringSecurity【发布时间】:2021-02-0813:49:59【问题描述】:我有一个SpringBoot应用程序,想为控制器编写集成测试。这是我的SecurityConfig:@Configurati... 查看详情

Spring Boot junit5 测试在本地工作(Windows)但不在 Jenkins(Linux)中

】SpringBootjunit5测试在本地工作(Windows)但不在Jenkins(Linux)中【英文标题】:SpringBootjunit5testsworkinglocally(Windows)butnotinJenkins(Linux)【发布时间】:2021-03-1409:44:45【问题描述】:我将在这里回答我自己的问题,以防它对其他人有用.... 查看详情

如何在 Junit5 / spring boot 中命令执行控制器测试类?

】如何在Junit5/springboot中命令执行控制器测试类?【英文标题】:HowtoorderexecutionofcontrollertestclassesinJunit5/springboot?【发布时间】:2021-05-0806:43:54【问题描述】:在我的实体中,我有双向的一对多关系,我想用mock.mvc编写集成测试。... 查看详情

JUnit 5 和具有不同扩展的继承,用于使用 TestContainers 进行 Spring Boot 集成测试

】JUnit5和具有不同扩展的继承,用于使用TestContainers进行SpringBoot集成测试【英文标题】:JUnit5andinheritancewithdifferentextensionsforSpringBootIntegrationtestwithTestContainers【发布时间】:2021-08-2116:50:30【问题描述】:简介:我们的产品需要对3... 查看详情

使用 JUnit 5 的 spring-boot-starter-test

】使用JUnit5的spring-boot-starter-test【英文标题】:spring-boot-starter-testwithJUnit5【发布时间】:2019-03-2213:46:28【问题描述】:从2.0.6开始使用spring-boot-starter-test会引入JUnit4依赖项。如何使用spring-boot-starter-test(通过Gradle),但使用JUnit5... 查看详情

在不创建上下文的情况下禁用 Spring 和 Junit5 测试

...:12【问题描述】:我们最近将测试框架迁移到JUnit5,但在使用@SpringJUnitConfig的测试中使用@Disabled(和ExecutionConditions)时遇到了一些问题:在Junit4@Ignor 查看详情

Spring Boot Test 无法使用 LocalServerPort 注释自动连接端口

】SpringBootTest无法使用LocalServerPort注释自动连接端口【英文标题】:SpringBootTestfailingtoautowireportwithLocalServerPortannotation【发布时间】:2018-10-1817:53:10【问题描述】:我正在运行SpringBoot2.0.1和Junit5。我正在尝试在集成测试中获取端口... 查看详情

Spring Boot 2 + Junit 5:带有@Value 的空值

】SpringBoot2+Junit5:带有@Value的空值【英文标题】:SpringBoot2+Junit5:nullwith@Value【发布时间】:2019-03-2522:26:57【问题描述】:我有一个带有SpringBoot2和Junit5的应用程序,现在我正在尝试进行测试。我有一个名为OrderService的类,它看起... 查看详情

正确配置 Spring Boot 2 和 JUnit 5

】正确配置SpringBoot2和JUnit5【英文标题】:ProperconfigurationofSpringBoot2andJUnit5【发布时间】:2018-08-0716:12:04【问题描述】:使用SpringBoot2.0.0.RC2。我写了一个配置类:@Configuration@ConditionalOnProperty("launchdarkly.sdkKey")publicclassLDClientConfigur 查看详情

测试容器在 Spring Boot 项目中启动两个容器而不是一个

...roject【发布时间】:2021-09-0504:27:02【问题描述】:我正在使用带有SpringBoot2.4和Junit5的Testcontainers1.15.3。当我运行测试时,testcontainers会启动第一个容器并执行flyway 查看详情

Gradle 找不到集成测试、JUnit 5 和 Spring Boot:没有找到给定的测试包括:

】Gradle找不到集成测试、JUnit5和SpringBoot:没有找到给定的测试包括:【英文标题】:GradleCannotFindIntegrationTest,JUnit5andSpringBoot:Notestsfoundforgivenincludes:【发布时间】:2020-11-0113:53:46【问题描述】:当我跑步时:./gradlew:integrationTest--te... 查看详情

是否有任何特殊配置可以将 SpringRunner 与 junit5 一起使用?

】是否有任何特殊配置可以将SpringRunner与junit5一起使用?【英文标题】:IsthereanyspecialconfigurationtouseSpringRunnerwithjunit5?[duplicate]【发布时间】:2020-07-2003:44:46【问题描述】:我的基于spring-boot框架的微服务项目和我所有的单元测试... 查看详情

Gradle 5 JUnit BOM 和 Spring Boot 版本不正确

...rsions【发布时间】:2019-07-0301:17:56【问题描述】:我正在使用Gradle5的BOM(材料清单)feature。这就是我对JUnit5依赖项的描述方式:testImplementation(enforcedPlatform("org.junit:junit-b 查看详情

在 Spring Boot 和 PostgreSQL 中使用 OneToMany 关系

】在SpringBoot和PostgreSQL中使用OneToMany关系【英文标题】:UsingOneToManyrelationinSpringBootandPostgreSQL【发布时间】:2021-06-0511:14:50【问题描述】:我有两个实体,帖子和评论。发布实体:@Entity@Table(name="posts")publicclassPost@Id@GeneratedValuepriva... 查看详情

使用 Spring Boot 和 Spring JDBC 在 oracle 中设置默认模式 = SOMETHING

】使用SpringBoot和SpringJDBC在oracle中设置默认模式=SOMETHING【英文标题】:Setdefaultschema=SOMETHINGinoracleusingSpringBootandSpringJDBC【发布时间】:2016-06-2216:17:53【问题描述】:我现在正在使用oracle和springjdbc,但我不想在我的sql语句中使用架... 查看详情

为啥 Spring Boot 使用单例容器方法生成 2 个测试容器?

】为啥SpringBoot使用单例容器方法生成2个测试容器?【英文标题】:WhydoesSpringBootspawn2testcontainerswithsingletoncontainerapproach?为什么SpringBoot使用单例容器方法生成2个测试容器?【发布时间】:2021-10-2802:16:06【问题描述】:我正在使用... 查看详情