20-SpringBoot 整合单元测试JUnit5

1.相关介绍

Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库, 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的测试引擎。

2.整合方法

  • 使用添加JUnit 5,添加对应的starter, 在pom.xml文件中添加依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>

注意:

  • SpringBoot 2.4 以上版本移除了默认对 Vintage 的依赖。如果需要兼容JUnit4需要自行引入(不能使用JUnit4的功能 @Test)
  • JUnit 5’s Vintage已经从spring-boot-starter-test从移除。如果需要继续兼容Junit4需要自行引入Vintage依赖:
    (不使用JUnit4可忽略)
<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>

Spring的JUnit 5的基本单元测试模板(Spring的JUnit4的是@SpringBootTest+@RunWith(SpringRunner.class)):

package com.limi.springboottest2;


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

@SpringBootTest
public class JTest5 {


    @Test
    public void test1(){
        System.out.println("123456");
    }
}

3.常用注解

  • @Test:表示方法是测试方法。但是与JUnit4的@Test不同,他的职责非常单一不能声明任何属性,拓展的测试将会由Jupiter提供额外测试
  • @ParameterizedTest:表示方法是参数化测试。
  • @RepeatedTest:表示方法可重复执行。
  • @DisplayName:为测试类或者测试方法设置展示名称。
  • @BeforeEach:表示在每个单元测试之前执行。
  • @AfterEach:表示在每个单元测试之后执行。
  • @BeforeAll:表示在所有单元测试之前执行。
  • @AfterAll:表示在所有单元测试之后执行。
  • @Tag:表示单元测试类别,类似于JUnit4中的@Categories。
  • @Disabled:表示测试类或测试方法不执行,类似于JUnit4中的@Ignore。
  • @Timeout:表示测试方法运行如果超过了指定时间将会返回错误。
  • @ExtendWith:为测试类或测试方法提供扩展类引用。

4.运行测试

JTest5

package com.limi.springboottest2;


import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.TimeUnit;

@DisplayName("junit5功能测试类")
@SpringBootTest
public class JTest5 {


    @DisplayName("测试displayname注解")
    @Test
    public void test1(){
        System.out.println("1");
    }

    //传入指定参数
    @ParameterizedTest
    @ValueSource(strings = {"参数1", "参数2"})
    public void test2(String str){
        System.out.println("2");
        System.out.println(str);
    }

    //当运行整个测试类时, 该方法不会被执行
    @Disabled
    @Test
    public void test3(){
        System.out.println("3");
    }

    //当运行整个测试类时, 该方法会被重复执行3次
    @RepeatedTest(3)
    @Test
    public void test4(){
        System.out.println("4");
    }

    //测试方法执行超出设置的时间, 抛出依次
    @Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
    @Test
    public void test5(){
        System.out.println("5");
        try {
            Thread.sleep(600);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test6(){
        System.out.println("6");
    }

    //每一个测试方法执行前都会触发该方法
    @BeforeEach
    void testBeforeEach() {
        System.out.println("有一个测试就要开始了...");
    }

    //每一个测试方法执行完成后都会触发该方法
    @AfterEach
    void testAfterEach() {
        System.out.println("有一个测试测试结束了...");
    }

    //当执行整个测试类时, 在第一个测试方法执行前会触发该方法
    @BeforeAll
    static void testBeforeAll() {
        System.out.println("所有测试就要开始了...");
    }

    //当执行测试类时, 所有测试方法执行完成后会触发该方法
    @AfterAll
    static void testAfterAll() {
        System.out.println("所有测试以及结束了...");

    }
}

在这里插入图片描述
在这里插入图片描述
注意:网络不好执行测试可能会报错:Failed to resolve org.junit.platform:junit-platform-launche

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值