2019 新版本idea Junit 5使用问题

1、问题

最近在使用idea的spring initializr构建的spring boot项目后,使用单元测试的时候出现如下问题:

测试部分代码
package com.example.demo1;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class Demo1ApplicationTests {

    @Test
    void contextLoads() {
        System.out.println("hello world!");

    }

}

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.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo1</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

点击运行contextLoads方法之后,出现以下问题

在这里插入图片描述

根据提示可以知道无法解析1.5.2版本的平台启动器(JUnit 5引入了概念Launcher,可用于发现,过滤和执行测试)

在这里插入图片描述

idea自动创建的spring boot项目junit的平台启动器是1.5.1的版本自然无法解析,需要手动导入1.5.2版本的平台启动器
解决:

把上面的1.5.2版本的平台平启动器依赖加入pom.xml,导入对应的包即可

<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.5.2</version>
    <scope>test</scope>
</dependency>
成功运行

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用IntelliJ IDEA进行JUnit 5单元测试配置时,你需要进行以下步骤: 1. 首先,确保你的项目中已经引入了JUnit Jupiter依赖。你可以通过在pom.xml文件(如果是Maven项目)或build.gradle文件(如果是Gradle项目)中添加以下代码来引入依赖: ```xml <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.1</version> <scope>test</scope> </dependency> ``` 2. 在你的测试类中,使用`@Test`注解来标记要执行的测试方法。这个注解来自JUnit Jupiter模块。 3. 确保你的项目使用了Java 8或更高版本,因为JUnit 5需要Java 8及以上的版本。 4. 打开IntelliJ IDEA,找到你的测试类,右击并选择"Run 'TestClassName'"来运行你的单元测试。 请注意,JUnit 5还包括JUnit Platform和JUnit Vintage。JUnit Platform提供了测试引擎API,用于开发在JUnit平台上运行的新测试框架。JUnit JupiterJUnit 5的一部分,它提供了新的注解和测试引擎的实现,可以用于测试使用新注解编写的测试代码。JUnit Vintage用于支持JUnit 5平台上运行使用JUnit 3和JUnit 4编写的测试用例。在大多数情况下,你只需要使用JUnit Jupiter模块进行单元测试。 希望这些信息对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [JUnit 5 单元测试教程](https://blog.csdn.net/u013735734/article/details/127915358)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [JUnit 5单元测试(一)—— 基本配置](https://blog.csdn.net/qq_33697094/article/details/129594054)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值