【发布时间】:2014-10-27 06:00:22
【问题描述】:
在我的代码中,我不想加载 XXApplicationConfig 类中定义的所有 bean。
XXApplicationConfig 是一个@Configuration 注释文件,其中定义了一堆spring bean。
所以,我想在测试时仅从 XXApplicationConfig 类加载 AppBean,以减少加载测试时间并区分我正在测试的内容。我还想使用 XXApplicationConfig 类加载该类,以确保定义的 bean 配置也是正确的。
这是我用来测试 AppBean 类的测试类(修改后的)。
您能否告诉我这是否是正确的方法并建议如何改进?目前,这种方法似乎正在奏效。但是,不确定它是否是接近它的正确方法。
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class ApplicationTest {
@Configuration
@PropertySources(value = {@PropertySource("classpath:test.properties")})
static class MyTestConfiguration {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceHolderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
public XXApplicationConfig xxAppConfig() {
return new XXApplicationConfig();
}
@Bean
public CustomTestService customTestService() {
return new CustomTestService();
}
@Bean
public AppBean appBean() throws Exception {
return XXApplicationConfig().appBean();
}
}
@Autowired
private AppBean appBean;
@Test
public void testAppBean() {
test appBean.doSomething();
}
}
【问题讨论】: