代码拉取完成,页面将自动刷新
@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");
}
}
在没有@ActiveProfiles的时候,profile=default和没有设定profile的Bean会被加载到。
@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");
}
}
当使用了@ActiveProfiles的时候,profile匹配的和没有设定profile的Bean会被加载到。
@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同样也可以和@SpringBootTest配合使用,这里就不举例说明了。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。