[英]Use different Spring test context configuration for different test methods
我们有一个基于Spring的JUnit测试类,它使用内部测试上下文配置类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ServiceTest.Config.class)
public class ServiceTest {
@Test
public void someTest() {
...
@Configuration
@PropertySource(value = { "classpath:application.properties" })
@ComponentScan({ "..." })
public static class Config {
...
最近已向Service类引入了新功能,应将相关测试添加到ServiceTest中。 但是,这些还需要创建一个不同的测试上下文配置类(现有Config类的内部相当复杂,如果可能的话,将其更改为新旧测试似乎非常困难)
有没有办法实现一个测试类中的某些测试方法将使用一个配置类,而其他方法将使用另一个? @ContextConfiguration
似乎只适用于类级别,因此解决方案可能是为新测试创建另一个测试类,它将使用自己的上下文配置类; 但这意味着通过两个不同的测试类覆盖相同的Service类
有了Aaron关于手动构建上下文的建议,我找不到任何好的例子,所以在花了一些时间让它工作之后,我想我会发布一个简单版本的代码,以防它帮助其他人:
class MyTest {
@Autowired
private SomeService service;
@Autowired
private ConfigurableApplicationContext applicationContext;
public void init(Class<?> testClass) throws Exception {
TestContextManager testContextManager = new TestContextManager(testClass);
testContextManager.prepareTestInstance(this);
}
@After
public void tearDown() throws Exception {
applicationContext.close();
}
@Test
public void test1() throws Exception {
init(ConfigATest.class);
service.doSomething();
// assert something
}
@Test
public void test2() throws Exception {
init(ConfigBTest.class);
service.doSomething();
// assert something
}
@ContextConfiguration(classes = {
ConfigATest.ConfigA.class
})
static class ConfigATest {
static class ConfigA {
@Bean
public SomeService someService() {
return new SomeService(new A());
}
}
}
@ContextConfiguration(classes = {
ConfigBTest.ConfigB.class
})
static class ConfigBTest {
static class ConfigB {
@Bean
public SomeService someService() {
return new SomeService(new B());
}
}
}
}
当我必须解决这个问题时,我会使用这些方法:
请记住,注释只能解决一般情况。 当你离开共同点时,你将不得不复制他们的部分或全部工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.