栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot 单元测试(一)JUnit5

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot 单元测试(一)JUnit5

前言

Spring Boot 2.2.0 版本开始引入 JUnit5 作为单元测试默认库,在此之前版本spring-boot-starter-test使用 JUnit4,Spring Boot 2.2.0 版本之后替换成 JUnit Jupiter。

Spring Boot 工程创建成功后自动生成XxxApplicationTests单元测试类,此类的作用是检查应用程序上下文能否正常启动。@SpringBootTest注解作用是查找带@SpringBootApplication注解的配置类,使用该类启动应用程序上下文。

package com.example.demo;

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

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}
复制代码
JUnit 5

JUnit5 = JUnit Platform + JUnit Jupiter + JUnit Vintage

  • JUnit Platform:JVM上启动测试框架的基础,不仅支持JUnit自制的测试引擎,也可以接入其他测试引擎。
  • JUnit Jupiter:JUnit5 新特性的核心,内部包含一个测试引擎在 JUnit Platform 上运行。
  • JUnit Vintage:提供了兼容 JUnit 4.x、3.x 的测试引擎。Spring Boot 2.4 以上版本移除了对 Vintage 的默认依赖,如果要使用 JUnit4 需要自己引入 Vintage。
常用注解
  • @Test:只能用在方法上,表示该方法为测试方法。

    @Test
    public void test() {
        Assertions.assertTrue(false);
    }
    复制代码
  • @ParameterizedTest:参数化测试,使用不同的参数多次运行。

    package com.example.demo;
    
    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.params.ParameterizedTest;
    import org.junit.jupiter.params.provider.ValueSource;
    
    public class DemoUnitTest {
    
        @ParameterizedTest
        @ValueSource(ints = {1, 10, 100})
        public void test(int value) {
            Assertions.assertTrue(value > 0 && value <= 100);
        }
    }
    复制代码
  • @RepeatedTest:重复测试,括号中的值为重复的次数。

    @RepeatedTest(5)
    public void test() {
        Assertions.assertTrue(true);
    }
    复制代码
  • @DisplayName:为测试类或测试方法设置名称,支持 emoji。

    package com.example.demo;
    
    import org.junit.jupiter.api.Assertions;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.Test;
    
    @DisplayName("JUnit5 Demo")
    public class DemoUnitTest {
    
        @Test
        @DisplayName("第一个测试方法")
        public void test() {
            Assertions.assertTrue(true);
        }
    }
    复制代码
  • @BeforeEach:在每个单元测试方法执行前执行。

    package com.example.demo;
    
    import org.junit.jupiter.api.BeforeEach;
    import org.junit.jupiter.api.Test;
    
    public class DemoUnitTest {
    
        @BeforeEach
        public void beforeEach() {
            System.out.println("method[beforeEach] executed.");
        }
     
        @Test
        public void test1() {
            System.out.println("Unit test 1 executed.");
        }
    
        @Test
        public void test2() {
            System.out.println("Unit test 2 executed.");
        }
    }
    复制代码

    输出结果

    method[beforeEach] executed.
    Unit test 1 executed.
    method[beforeEach] executed.
    Unit test 2 executed.
    复制代码
  • @AfterEach:在每个单元测试方法执行完成后执行。

    package com.example.demo;
    
    import org.junit.jupiter.api.AfterEach;
    import org.junit.jupiter.api.Test;
    
    public class DemoUnitTest {
    
        @AfterEach
        public void AfterEach() {
            System.out.println("method[beforeEach] executed.");
        }
     
        @Test
        public void test1() {
            System.out.println("Unit test 1 executed.");
        }
    
        @Test
        public void test2() {
            System.out.println("Unit test 2 executed.");
        }
    }
    复制代码

    输出结果

    Unit test 1 executed.
    method[AfterEach] executed.
    Unit test 2 executed.
    method[AfterEach] executed.
    复制代码
  • @BeforeAll:在所有单元测试方法执行前执行,只能使用在静态方法上。

    package com.example.demo;
    
    import org.junit.jupiter.api.BeforeAll;
    import org.junit.jupiter.api.Test;
    
    public class DemoUnitTest {
    
        @BeforeAll
        public static void beforeAll() {
            System.out.println("method[beforeAll] executed.");
        }
     
        @Test
        public void test1() {
            System.out.println("Unit test 1 executed.");
        }
    
        @Test
        public void test2() {
            System.out.println("Unit test 2 executed.");
        }
    }
    复制代码

    输出结果

    method[beforeAll] executed.
    Unit test 1 executed.
    Unit test 2 executed.
    复制代码
  • @AfterAll:在所有单元测试方法执行完成后执行,只能使用在静态方法上。

    package com.example.demo;
    
    import org.junit.jupiter.api.AfterAll;
    import org.junit.jupiter.api.Test;
    
    public class DemoUnitTest {
    
        @AfterAll
        public static void afterAll() {
            System.out.println("method[afterAll] executed.");
        }
     
        @Test
        public void test1() {
            System.out.println("Unit test 1 executed.");
        }
    
        @Test
        public void test2() {
            System.out.println("Unit test 2 executed.");
        }
    }
    复制代码

    输出结果

    Unit test 1 executed.
    Unit test 2 executed.
    method[afterAll] executed.
    复制代码
  • @Tag:标注单元测试类别。

  • @Disabled:禁用不执行,类似于 JUnit4 的@Ignore。

  • @Timeout:测试方法超时设置,超过指定时间(单位:秒)则会执行失败。

    @Test
    @Timeout(1)
    public void test1() throws InterruptedException {
        Thread.currentThread().sleep(900);
        Assertions.assertEquals(0x111, 273);
    }
    复制代码
  • @ExtendWith:为测试类或测试方法提供扩展类引用,用以取代旧版本中的RunWith注解,不过在 Spring Boot 环境如果没有特别要求无需额外配置,因为@SpringBootTest中已经有了。

断言

JUnit5 断言使用org.junit.jupiter.api.Assertions的静态方法。 除此之外还可以使用 AssertJ(org.assertj.core.api.Assertions的assertThat方法)。



链接:https://juejin.cn/post/7041110340720934919
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666536.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号