在我的spring boot应用程序上,我想在所有@Configuration
测试中仅使用测试配置(尤其是我的@EnableAuthorizationServer
@Configuration
类)覆盖我的一个类。
到目前为止,在对Spring Boot测试功能和Spring Integration测试功能进行了概述之后,还没有一个简单的解决方案浮出水面:
@TestConfiguration
:用于扩展,而不是覆盖;@ContextConfiguration(classes=…)
并@SpringApplicationConfiguration(classes =…)
让我重写整个配置,而不仅仅是一个类;@Configuration
a内部的类@Test
来覆盖默认配置,但未提供示例。有什么建议?
内部测试配置
用于测试的内部@Configuration的示例:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeTest {
@Configuration
static class ContextConfiguration {
@Bean
@Primary //may omit this if this is the only SomeBean defined/visible
public SomeBean someBean () {
return new SomeBean();
}
}
@Autowired
private SomeBean someBean;
@Test
public void testMethod() {
// test
}
}
可重复使用的测试配置
如果您希望将测试配置重用于多个测试,则可以使用Spring Profile定义一个独立的Configuration类@Profile("test")
。然后,让您的测试班级通过激活配置文件@ActiveProfiles("test")
。查看完整的代码:
@RunWith(SpringRunner.class)
@SpringBootTests
@ActiveProfiles("test")
public class SomeTest {
@Autowired
private SomeBean someBean;
@Test
public void testMethod() {
// test
}
}
@Configuration
@Profile("test")
public class TestConfiguration {
@Bean
@Primary //may omit this if this is the only SomeBean defined/visible
public SomeBean someBean() {
return new SomeBean();
}
}
@基本的
在@Primary
对bean定义标注为确保这一具有优先权如果不止一个被发现。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句