Springboot整合Junit5

1 SpringBoot整合Junit

(一)junit5 介绍

Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库

作为最新版本的JUnit框架,JUnit5与之前版本的Junit框架有很大的不同。由三个不同子项目的几个不同模块组成。

JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

JUnit Platform: Junit Platform是在JVM上启动测试框架的基础,不仅支持Junit自制的测试引擎,其他测试引擎也都可以接入。

JUnit Jupiter: JUnit Jupiter提供了JUnit5的新的编程模型,是JUnit5新特性的核心。内部 包含了一个测试引擎,用于在Junit Platform上运行。

JUnit Vintage: 由于JUint已经发展多年,为了照顾老的项目,JUnit Vintage提供了兼容JUnit4.x,Junit3.x的测试引擎。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IQ7rB8di-1681698840475)(./img/Junit5.jpg)]

(二)SpringBoot整合Junit

在这里插入图片描述

1 构建工程添加依赖

<!--junit5版本,默认不兼容Junit4 -->
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

<!--如果要继续兼容 junit4,自行引入Vintage-->
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

在这里插入图片描述

2 创建测试类

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


class JunitdemoApplicationTests{

    @Test
    void contextLoads() {
    }

}

3 测试类上添加注解

@SpringBootTest

4 测试类注入测试对象

@SpringBootTest
class JunitdemoApplicationTests{
    @Autowired
    private ApplicationContext applicationContext;
    @Test
    void contextLoads() {
        System.out.println(applicationContext);
    }
}

在这里插入图片描述

posted @   要买CT5的小曹  阅读(110)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
点击右上角即可分享
微信分享提示