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


声明:本文转载自https://my.oschina.net/sqq/blog/1525903,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

码云地址

@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");   }    @Bean   public Bar bar() {     return new Bar("no profile");   }  } 

例子1:不使用ActiveProfiles

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

源代码ActiveProfileTest

@ContextConfiguration(classes = Config.class) public class ActiveProfileTest extends AbstractTestNGSpringContextTests {    @Autowired   private Foo foo;    @Autowired   private Bar bar;    @Test   public 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 {    @Autowired   private Foo foo;    @Autowired   private Bar bar;    @Test   public void test() {     assertEquals(foo.getName(), "product");     assertEquals(bar.getName(), "no profile");   }  } 

总结

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

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

参考文档

本文发表于2017年08月30日 10:36
(c)注:本文转载自https://my.oschina.net/sqq/blog/1525903,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 1936 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1