Spring、Spring Boot和TestNG测试指南 - @ActiveProfiles

news/2024/7/1 2:54:40

Github地址

@ActiveProfiles可以用来在测试的时候启用某些Profile的Bean。本章节的测试代码使用了下面的这个配置:

@Configuration
public class Config {@Bean@Profile("dev")public Foo fooDev() {return new Foo("dev");}@Bean@Profile("product")public Foo fooProduct() {return new Foo("product");}@Bean@Profile("default")public Foo fooDefault() {return new Foo("default");}@Beanpublic Bar bar() {return new Bar("no profile");}}

例子1:不使用ActiveProfiles

在没有@ActiveProfiles的时候,profile=default和没有设定profile的Bean会被加载到。

源代码ActiveProfileTest:

@ContextConfiguration(classes = Config.class)
public class ActiveProfileTest extends AbstractTestNGSpringContextTests {@Autowiredprivate Foo foo;@Autowiredprivate Bar bar;@Testpublic void test() {assertEquals(foo.getName(), "default");assertEquals(bar.getName(), "no profile");}}

例子2:使用ActiveProfiles

当使用了@ActiveProfiles的时候,profile匹配的和没有设定profile的Bean会被加载到。

源代码ActiveProfileTest:

@ContextConfiguration(classes = Config.class)
[@ActiveProfiles][doc-active-profiles]("product")
public class ActiveProfileTest extends AbstractTestNGSpringContextTests {@Autowiredprivate Foo foo;@Autowiredprivate Bar bar;@Testpublic void test() {assertEquals(foo.getName(), "product");assertEquals(bar.getName(), "no profile");}}

总结

  • 在没有@ActiveProfiles的时候,profile=default和没有设定profile的Bean会被加载到。

  • 当使用了@ActiveProfiles的时候,profile匹配的和没有设定profile的Bean会被加载到。

@ActiveProfiles同样也可以和@SpringBootTest配合使用,这里就不举例说明了。

参考文档

  • Spring Framework Testing

  • Spring Boot Testing


http://lihuaxi.xjx100.cn/news/239714.html

相关文章

linux下接口持续集成,部署jenkins持续集成工具

1、Linux安装配置jdk环境1.1、上传到 Linux 服务器;例如:上传至: cd /usr/local1.2、解压:rpm -ivh jdk-8u111-linux-x64.rpm1.3、环境变量配置cd /etc在etc下,找到 profile文件,增加如下如下配置&#xff…

MySQL数据类型--------浮点类型实战

1. 背景 * MySQL支持的浮点类型中有单精度类型(float), 双精度类型(double),和高精度类型(decimal),在数字货币类型中推荐使用高精度类型(decimal)来进行应用. * MySQL浮点型和定点型可以用类型名称后加(M,D)来表示&am…

windows指令

为什么80%的码农都做不了架构师?>>> C:\Windows\System32\drivers\etc nbtstat -a 1.7.2.2s 检查该IP的主机名称 WExNmU5Z windows启动配置界面 在“运行”中输入“msconfig mstsc -admin 远程 虚拟机的判断:如果有vmtoolsd.exe进程就是虚拟…

1:1 人脸比对 开源_Hacktoberfest:我的开源门户

1:1 人脸比对 开源by Maribel Duran通过Maribel Duran Hacktoberfest:我的开源门户 (Hacktoberfest: My Gateway to Open Source) “Individually, we are one drop. Together, we are an ocean.”“就个人而言,我们只是一滴滴。 在一起,我们…

c语言函数注释例子,C语言实例说明

原标题:C语言实例说明上一节,我们大致总揽了一个简单C程序的框架,程序如下:123456789#include /*引入头文件*/int main( void ) /*一个简单的C程序*/{int number; /*定义个名字叫做number的变量*/number2014; /*给number赋一个值*…

react组件样板_如何构建自己的React样板

react组件样板by Nick Karnik尼克卡尼克(Nick Karnik) 如何构建自己的React样板 (How to build your own React boilerplate) 什么是样板? (What is a Boilerplate?) In programming, the term boilerplate code refers to blocks of code used over and over aga…

私有vlan

一 拓扑图 二 配置私有vlan(pvlan) 只有VTP模式为透明模式,才能配置pvlan (1)配置pc1-4及s2(给测试用) pc1-4按照拓扑图上的说明配置 s2(config)#interface fastEthernet 0/1 s2(config-if)#switchport mode access s…

jfinal整合shiro回顾

2019独角兽企业重金招聘Python工程师标准>>> 目前jfinal使用shiro进行身份验证和授权的后台实现已完成,现在我再来总结下学习过程及代码实现过程。最近半年多项目开发都用.net,但又不甘心用了一年多的java,jfinal就这样被废弃&…