tom*_*per 1 java spring caching
我的代码有许多集成测试类- ServiceTest1
,ServiceTest2
...,ServiceTestN
。所有测试均使用相同的测试上下文(MyAppContext.xml
)在spring上执行。
正如在下面的代码段中所示,ServiceTest1
测试上下文需要其豆中的一个被覆盖,而这种改变是相关ONLY对ServiceTest1
。
执行后,ServiceTest1
将按预期工作。但是,当执行其他测试时(例如ServiceTest2
,在代码段中),它会因Spring初始化错误而失败,因为spring正在缓存MyAppContext.xml
测试上下文,并且 ServiceTest1
测试上下文操作与其他测试冲突。
我正在寻找一种避免缓存ServiceTest1
测试上下文的方法(例如,基于@ContextConfiguration使其缓存键唯一)。请注意,完全禁用缓存不是想要的解决方案。
有任何想法吗?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class ServiceTest1 {
// some tests ...
@Configuration
@ImportResource({"classpath*:MyAppContext.xml"})
static class Context {
@Bean
@Primary
ServiceOne serviceOne() {
// instantiate something ...
}
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:MyAppContext.xml"})
public class ServiceTest2 {
// some tests ...
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
编辑:
Spring初始化错误是:
java.lang.IllegalStateException:无法加载ApplicationContext
...
由以下原因引起:org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:无法从URL位置导入bean定义[类路径:/ WEB-INF / applicationContext-security]
令人反感的资源:URL [file:/path/to/MyAppContext.xml];嵌套的异常是org.springframework.beans.factory.parsing.BeanDefinitionParsingException:配置问题:检测到重复。
令人反感的资源:类路径资源[WEB-INF / applicationContext-security.xml]
使用@DirtiesContext
注释,在类级别添加注释。
测试类执行后,它将刷新spring应用程序上下文。
文档:http : //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/annotation/DirtiesContext.html
归档时间: |
|
查看次数: |
4785 次 |
最近记录: |