七、springboot 单元测试阶段 (1.JUnit5 的变化 )

news/2024/5/19 4:50:08 标签: spring boot, 单元测试, java, 与数据库交互, JUnit5

1、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的测试引擎。

注意:

SpringBoot 2.4 以上版本移除了默认对 Vintage 的依赖。如果需要兼容junit4需要自行引入(不能使用junit4的功能 @Test

JUnit 5’s Vintage Engine Removed from spring-boot-starter-test,如果需要继续兼容junit4需要自行引入vintage

<dependency>  //这个是使用4的加入的依赖
    <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>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
</dependency>

现在版本:

@SpringBootTest

class Boot05WebAdminApplicationTests {
    @Test
    void contextLoads() {
    }
}

以前:

@SpringBootTest + @RunWith(SpringTest.class)

SpringBoot整合Junit以后。

  • 编写测试方法:@Test标注(注意需要使用junit5版本的注解)
  • Junit类具有Spring的功能,@Autowired、比如 @Transactional 标注测试方法,测试完成后自动回滚

http://www.niftyadmin.cn/n/1712964.html

相关文章

七、springboot 单元测试阶段 (2、JUnit5常用注解,3、断言(assertions) )

2、JUnit5常用注解 JUnit5的注解与JUnit4的注解有所变化 JUnit 5 User Guide Test :表示方法是测试方法。但是与JUnit4的Test不同&#xff0c;他的职责非常单一不能声明任何属性&#xff0c;拓展的测试将会由Jupiter提供额外测试ParameterizedTest :表示方法是参数化测试&…

七、springboot 单元测试阶段 (4、前置条件(assumptions)5、嵌套测试 6、参数化测试 7、迁移指南)

目录 4、前置条件&#xff08;assumptions&#xff09; 5、嵌套测试 6、参数化测试 7、迁移指南 4、前置条件&#xff08;assumptions&#xff09; JUnit 5 中的前置条件&#xff08;assumptions【假设】&#xff09;类似于断言&#xff0c;不同之处在于不满足的断言会使得测…

八. springboot 的指标监控 (1.SpringBoot Actuator 2.Actuator Endpoint)

目录 1、SpringBoot Actuator 1.1、简介 1.2、1.x与2.x的不同 1.3、如何使用 1.4、可视化 2、Actuator Endpoint 2.1、最常使用的端点 2.2、Health Endpoint 2.3、Metrics Endpoint 2.4、管理Endpoints 2.4.1、开启与禁用Endpoints 2.4.2、暴露Endpoints 1、SpringBoot Actua…

Occlusion

简介&#xff1b; 遮挡剔除&#xff1a;剔除视椎体内被其他游戏对象所遮住的物体 静态物体的遮挡剔除&#xff1a; 将需要自动遮挡的对象勾选 为Occluder Static/Occludee Static&#xff0c;然后在 Occlusion 中进行简单的设置 Bake 即可。最后可以在 Visualization 模式下…

八. springboot 的指标监控 (3、定制 Endpoint )

3、定制 Endpoint 3.1、定制 Health 信息 import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; Component public class MyHealthIndicator implemen…

字符编码笔记

ASCII码&#xff1a;一个英文字母&#xff08;不分大小写&#xff09;占一个字节的空间&#xff0c;一个中文汉字占两个字节的空间。一个二进制数字序列&#xff0c;在计算机中作为一个数字单元&#xff0c;一般为8位二进制数&#xff0c;换算为十进制。最小值-128&#xff0c;…

unity中so文件的导出及其使用

1、在unity中的使用 在 Assets下创建Plugins/Android文件夹&#xff0c;里面可以区分arm64-v8a&#xff0c;armeabi-v7a&#xff0c;x86针对Android各个平台的so文件 2、各个平台的区别 armv7架构---是arm的32位 armv8架构-----是arm的64位 x86指令是很早的指令集 Android…

Unity中路径的使用

1、Resources文件夹&#xff1a;里面文件会原封不动的达到apk包中&#xff0c;其中的文件可以读&#xff0c;写 StreamingAssets文件夹&#xff1a;里面文件会原封不动的达到apk包中&#xff0c;其中的文件可以读&#xff0c;不能写入 persistentDataPath文件夹&#xff1a;路…