Java 如何在spring测试中重用ContextConfiguration注释的位置值
我有很多关于spring的测试。我们的应用程序上下文由几个xml文件组成。大概是这样的:Java 如何在spring测试中重用ContextConfiguration注释的位置值,java,spring,junit,spring-test,Java,Spring,Junit,Spring Test,我有很多关于spring的测试。我们的应用程序上下文由几个xml文件组成。大概是这样的: @ContextConfiguration( locations = { "classpath:aaaaa.xml", "classpath:bbbbb.xml"}) import org.springframework.context.annotation.Configuration; import org.springframework.context.annotati
@ContextConfiguration( locations = {
"classpath:aaaaa.xml",
"classpath:bbbbb.xml"})
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource(value = {
"classpath:aaaaa.xml",
"classpath:bbbbb.xml"})
public class IntegrationTestConfiguration {
}
Spring默认缓存上下文,这可以防止每次测试都重新创建上下文,从而提高性能。为了享受这个特性,我必须在每次测试中使用完全相同的“位置”
如何在测试之间共享这种注释?我知道我可以创建一个父类,所有测试都可以扩展它。然而,有时我确实需要扩展另一个类。我试图将字符串[]放入常量,但注释不能将常量作为值,因为这是禁止的
所以问题是如何在没有父类的测试之间共享一组spring上下文xml位置?我已经找到了答案。我必须创建这样一个类:
@ContextConfiguration( locations = {
"classpath:aaaaa.xml",
"classpath:bbbbb.xml"})
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource(value = {
"classpath:aaaaa.xml",
"classpath:bbbbb.xml"})
public class IntegrationTestConfiguration {
}
然后在测试课上,我必须说:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IntegrationTestConfiguration.class )
public class MyTest
就这样。我已经找到了。我必须创建这样一个类:
@ContextConfiguration( locations = {
"classpath:aaaaa.xml",
"classpath:bbbbb.xml"})
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@ImportResource(value = {
"classpath:aaaaa.xml",
"classpath:bbbbb.xml"})
public class IntegrationTestConfiguration {
}
然后在测试课上,我必须说:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = IntegrationTestConfiguration.class )
public class MyTest
就是这样。如果我们不使用xml配置,我们使用类如何处理它?@Import({ConfigA.class,ConfigB.class,ConfigC.class})org.springframework.context.annotation.Import注释如果我们不使用xml配置,我们使用类如何处理它?@Import({ConfigA.class,ConfigB.class,ConfigC.class})org.springframework.context.annotation.Import annotation