所以,我正在做一些需要使用注解进行依赖注入的 Spring 测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class BeanTest {
@Autowired
private SomeService someService;
@Configuration
static class ContextConfiguration {
@Bean
public SomeService someService() {
return new SomeService();
}
}
}
我真的不想在每次测试中都重复这段代码,但我尝试创建一个包含配置的基类:
@Configuration
class MyContextConfiguration {
@Bean
public SomeService someService() {
return new SomeService();
}
}
并由此得出:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class BeanTest {
@Autowired
private SomeService someService;
@Configuration
static class ContextConfiguration extends MyContextConfiguration {}
}
似乎不起作用。任何人都可以建议一种方法来干燥这个吗?
谢谢!