资讯专栏INFORMATION COLUMN

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

CloudwiseAPM / 3387人阅读

摘要:地址可以用来在测试的时候启用某些的。源代码总结在没有的时候,和没有设定的会被加载到。同样也可以和配合使用,这里就不举例说明了。

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

  @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配合使用,这里就不举例说明了。

参考文档

Spring Framework Testing

Spring Boot Testing

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/70271.html

相关文章

  • SpringSpring BootTestNG测试指南 - 测试Spring MVC

    摘要:地址提供了,能够很方便的来测试。同时也提供了更进一步简化了测试需要的配置工作。本章节将分别举例说明在不使用和使用下如何对进行测试。例子测试的关键是使用对象,利用它我们能够在不需启动容器的情况下测试的行为。 Github地址 Spring Testing Framework提供了Spring MVC Test Framework,能够很方便的来测试Controller。同时Spring...

    andong777 评论0 收藏0
  • SpringSpring BootTestNG测试指南 - @OverrideAutoConfi

    摘要:因为只有这样才能够在测试环境下发现生产环境的问题,也避免出现一些因为配置不同导致的奇怪问题。而方法则能够不改变原有配置不提供新的配置的情况下,就能够关闭。 Github地址 在Chapter 1: 基本用法 - 使用Spring Boot Testing工具里提到: 除了单元测试(不需要初始化ApplicationContext的测试)外,尽量将测试配置和生产配置保持一致。比如如果生产...

    elisa.yang 评论0 收藏0
  • SpringSpring BootTestNG测试指南 - @JsonTest

    摘要:地址是提供的方便测试序列化反序列化的测试工具,在的文档中有一些介绍。例子简单例子源代码见使用通包下的文件测试结果是否正确或者使用基于的校验例子测试可以用来测试。这个例子里使用了自定义的测试代码例子使用事实上也可以配合一起使用。 Github地址 @JsonTest是Spring Boot提供的方便测试JSON序列化反序列化的测试工具,在Spring Boot的文档中有一些介绍。 需要注...

    Hegel_Gu 评论0 收藏0
  • SpringSpring BootTestNG测试指南 - 使用Spring Testing工具

    摘要:源代码见需要注意的是,如果是专供某个测试类使用的话,把它放到外部并不是一个好主意,因为它有可能会被扫描到,从而产生一些奇怪的问题。 Github地址 既然我们现在开发的是一个Spring项目,那么肯定会用到Spring Framework的各种特性,这些特性实在是太好用了,它能够大大提高我们的开发效率。那么自然而然,你会想在测试代码里也能够利用Spring Framework提供的特...

    Maxiye 评论0 收藏0
  • SpringSpring BootTestNG测试指南 - 共享测试配置

    摘要:地址在使用工具中提到在测试代码之间尽量做到配置共用。本章将列举几种共享测试配置的方法我们可以将测试配置放在一个里,然后在测试或中引用它。也可以利用的及自定义机制,提供自己的用在测试配置上。 Github地址 在使用Spring Boot Testing工具中提到: 在测试代码之间尽量做到配置共用。...能够有效利用Spring TestContext Framework的缓存机制,Ap...

    CHENGKANG 评论0 收藏0

发表评论

0条评论

CloudwiseAPM

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<