@SpringBootTest注解 --基于SpringBoot2.5.7版本

SpringBootTest介绍

可以在运行基于Spring Boot的测试的测试类上指定的注释。在常规的Spring TestContext框架之上提供了以下特性:

  • 默认提供SpringBootContextLoader作为ContextLoader,也通过 @ContextConfiguration(loader=…)来自定义

  • 若没有显示指定,将查找嵌套的@Configuration类,然后返回到SpringBootConfiguration搜索配置

  • 允许使用properties属性定义自定义环境属性。

  • 允许使用args属性定义应用程序参数。

  • 支持不同webEnvironment模式,包括自定义运行web服务器监听或默认为随机端口

  • web服务模式下,自动注册一个TestRestTemplate和/或WebTestClient bean用于web测试

配置名称 备注
value 配置属性
properties 配置属性
args 应用启动参数
classes 指定加载容器上下文配置类,等同于@ContextConfiguration中的class,若没有显示指定,将查找嵌套的@Configuration类,然后返回到SpringBootConfiguration搜索配置

关于 aliasFor可以参考 spring 官方

SpringBootTest源码

@Target(ElementType.TYPE)//注解只能用于Class, interface (including annotation type), or enum declaration
@Retention(RetentionPolicy.RUNTIME)//注释将由编译器记录在类文件中,并在运行时由VM保留,因此可以反射性地读取它们。
@Documented
@Inherited //允许子类继承
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith(SpringExtension.class)
public @interface SpringBootTest {/*** Alias for {@link #properties()}.* @return the properties to apply*/@AliasFor("properties")String[] value() default {};/*** Properties in form {@literal key=value} that should be added to the Spring* {@link Environment} before the test runs.* @return the properties to add*/@AliasFor("value")String[] properties() default {};/*** Application arguments that should be passed to the application under test.* @return the application arguments to pass to the application under test.* @see ApplicationArguments* @see SpringApplication#run(String...)* @since 2.2.0*/String[] args() default {};/*** The <em>component classes</em> to use for loading an* {@link org.springframework.context.ApplicationContext ApplicationContext}. Can also* be specified using* {@link ContextConfiguration#classes() @ContextConfiguration(classes=...)}. If no* explicit classes are defined the test will look for nested* {@link Configuration @Configuration} classes, before falling back to a* {@link SpringBootConfiguration @SpringBootConfiguration} search.* @see ContextConfiguration#classes()* @return the component classes used to load the application context*/Class<?>[] classes() default {};/*** The type of web environment to create when applicable. Defaults to* {@link WebEnvironment#MOCK}.* @return the type of web environment*/WebEnvironment webEnvironment() default WebEnvironment.MOCK;/*** An enumeration web environment modes.*/enum WebEnvironment {/*** Creates a {@link WebApplicationContext} with a mock servlet environment if* servlet APIs are on the classpath, a {@link ReactiveWebApplicationContext} if* Spring WebFlux is on the classpath or a regular {@link ApplicationContext}* otherwise.*/MOCK(false),/*** Creates a web application context (reactive or servlet based) and sets a* {@code server.port=0} {@link Environment} property (which usually triggers* listening on a random port). Often used in conjunction with a* {@link LocalServerPort @LocalServerPort} injected field on the test.*/RANDOM_PORT(true),/*** Creates a (reactive) web application context without defining any* {@code server.port=0} {@link Environment} property.*/DEFINED_PORT(true),/*** Creates an {@link ApplicationContext} and sets* {@link SpringApplication#setWebApplicationType(WebApplicationType)} to* {@link WebApplicationType#NONE}.*/NONE(false);private final boolean embedded;WebEnvironment(boolean embedded) {this.embedded = embedded;}/*** Return if the environment uses an {@link ServletWebServerApplicationContext}.* @return if an {@link ServletWebServerApplicationContext} is used.*/public boolean isEmbedded() {return this.embedded;}}}

demo案例

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,properties = "server.port=8081", args = "--spring.application.name=demoTest",classes = AppApplication.class)
@Import(MyConfig.class)
public class SpringBootDemoTest {@Autowiredprivate ApplicationContext applicationContext;@Value("${spring.application.name}")private String appName;@Autowired(required = false)private MyConfig myConfig;@Testvoid AppApplicationTest() {assert ("demoTest".equals(appName));assertThat(myConfig).isNotNull().extracting("cfgName").isEqualTo("11");assertThat(applicationContext).isNotNull();}
}
import lombok.Getter;
import lombok.Setter;@Getter
@Setter
public class MyConfig {private String cfgName = "11";
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author bearboy80*/
@SpringBootApplication
public class AppApplication {public static void main(String[] args) {SpringApplication.run(AppApplication.class, args);}
}

SpringBootTest注解相关推荐

  1. SpringBoot单元测试的@RunWith与@SpringBootTest注解

    SpringBoot测试类注解示例: import org.junit.runner.RunWith; import org.springframework.boot.test.context.Spr ...

  2. @SpringBootTest注解进行单元测试

    1.首先我们通过idea创建一个Springboot项目,项目目录生成后,默认都会带main和test目录,如下: 2.我们在test目录下创建测试类,正常情况下创建项目的时候会自带生成对应的测试类, ...

  3. @SpringBootTest注解进行单元测试无法运行

    1:用idea新建一个项目 2:在测试类下建一个方法,发现方法没法运行 查看资料之后发现是需要在对应的方面名称前面和类名前面加上public修饰符即可,需要测试那个方法执行哪个方法就行 3:加了 pu ...

  4. SpringBoot Test及注解详解(含Mockito)

    一.版本差异 Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库,在 Spring Boot 2.2.0 版本之前,spring-boot-starter-test ...

  5. 测试案例中@SpringBootTest与@RunWith**的含义

    背景:平常都是写功能,写业务代码忽略了对测试案例的理解,借此机会梳理记录一下测试案例中常用到的的几个注解. 一:@SpringBootTest 作用是加载ApplicationContext,启动sp ...

  6. SpringBoot中使用AMQ的两种方式二(Java配置、注解方式)

    使用@JmsListener注解方式 1. 工程目录 2. 引入依赖 <?xml version="1.0" encoding="UTF-8"?> ...

  7. JAVA_SpringBoot中涉及的注解

    SpringBoot注解 动吧 new 对象是一个耗时的过程, spring 是一个资源整合框架. @SpringBootApplication 由此注解(@SpringBootApplication ...

  8. Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件

    本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 Starter 组件 摘录:读书是读完这些文字还要好好用心去想想,写书也一样,做任何事也一样 图 2 第二章目录结构图 第 2 章 Spr ...

  9. springboot中文文档_登顶 Github 的 Spring Boot 仓库!艿艿写的最肝系列

    源码精品专栏 中文详细注释的开源项目 RPC 框架 Dubbo 源码解析 网络应用框架 Netty 源码解析 消息中间件 RocketMQ 源码解析 数据库中间件 Sharding-JDBC 和 My ...

最新文章

  1. 使用putty远程linux服务
  2. RTC是DS1339,驱动采用的是rtc-ds1307.c
  3. 全国计算机水平考试技巧,全国计算机等级考试上机考试应试技巧
  4. php7与apache整合,apache集成php7.3.5的详细步骤
  5. 数据库连接串你知道多少
  6. 微信数据解密-dat查看-免费dat转图片
  7. Java订单接入支付宝二 支付回调
  8. Photoshop如何调整证件照背景色
  9. c#语言模拟键盘输入,C#模拟键盘按键的三种方式实现
  10. 【POJ 3580】 SuperMemo
  11. 研究:多因素影响粮食安全 应早做规划避免粮食短缺
  12. 每日一题---摔手机
  13. 使用 Python 生成迷宫
  14. HDU 6441 Find Integer
  15. 羽素携手维琪共展科研实力,造护肤“芯”产链
  16. 如何卸载Android手机内置应用
  17. 在iview中render函数使用Switch功能
  18. jQuery实现省略指定多余文字
  19. cloudant_多租户服务的Cloudant最佳做法
  20. vue项目导出EXCEL功能

热门文章

  1. AutoCAD2014打开一闪而过解决方法
  2. 微信小程序实战--开发一个简单的快递单号查询
  3. 苹果手机拷贝功能 php,苹果iPhone上的“通用剪贴板”到底是什么?
  4. 业界分享 | 深度学习下的京东搜索召回技术
  5. 吃灰5年的iMac,今天才发现为啥那么卡。
  6. 百家号写的文章不给推荐怎么办?百家号怎么写出爆文?
  7. 利用c#+jquery+ichartjs生成统计图表
  8. mobl:针对移动Web开发的DSL【很详细】
  9. pythonr语言三种基本结构_如何轻松搞定数据科学面试:Python&R语言篇
  10. java区分手机号归属地_JAVA手机号码归属地查询