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

Springboot整合Junit5_第1张图片

1 构建工程添加依赖


<dependency>
  <groupId>org.springframework.bootgroupId>
  <artifactId>spring-boot-starter-testartifactId>
  <scope>testscope>
dependency>


<dependency>
    <groupId>org.junit.vintagegroupId>
    <artifactId>junit-vintage-engineartifactId>
    <scope>testscope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrestgroupId>
            <artifactId>hamcrest-coreartifactId>
        exclusion>
    exclusions>
dependency>

Springboot整合Junit5_第2张图片

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);
    }
}

Springboot整合Junit5_第3张图片

你可能感兴趣的:(Java毕生所学,spring,boot,junit,java)