文章

汇总:Spring Test with Jupiter

  1. Junit Jupiter
  2. Spring Test
    1. TestContext framework
    2. MockMvc
    3. client
  3. SpringBoot Test

Junit Jupiter

JUnit Jupiter是梦开始的地方。提供了@ExtendWith,让其他基于Jupiter的框架拥有拓展能力——主要是通过jupiter测试生命周期提供的回调,在test执行前维护框架自己的对象、注入对象,在执行后销毁对象。

Spring Test

spring-test借助jupiter的@ExtendWith,提供了SpringExtension读取自己关心的注解比如@ContextConfiguration,控制spring容器的启动和销毁。在测试开始之前(beforeAll)构建了自己的TestContext,启动ApplicationContext,维护需要的bean,并给test class注入相关bean。

测试的时候可以直接使用@ExtendWith(SpringExtension.class),也可以使用@SpringJUnitConfig

spring test还提供了@BootstrapWith拓展,能够让基于spring test的框架(springboot:正是在下)用自己的TestContextBootstrapper启动context。

TestContext framework

Spring Test - Spring TestContext Framework是spring test的核心架构。

MockMvc

Spring Mvc Test - MockMvc是测试spring mvc层的重要方法。

client

SpringBoot Test

spring-boot-test借助spring test的@BootstrapWith,提供了自己的TestContextBootstrapper接口实现SpringBootTestContextBootstrapper:以自己的方式搜集配置类和配置文件,启动context。

测试的时候一般不直接用@BootstrapWith(SpringBootTestContextBootstrapper.class),经常使用继承它的一些注解,比如@SpringBootTest@WebMvcTest等。

本文由作者按照 CC BY 4.0 进行授权