我对 spock 测试期间的上下文加载有疑问。我的类(class)看起来像这样(我有多个使用相同上下文的类(class)):
@ContextConfiguration(locations="classpath:context/mycontext.xml")
class SampleSpec extends Specification {
//implementation ommited
}
问题是当我单独运行测试时 (mvn clean test -Dtest=SampleSpec) 一切都按预期运行。但是当我一次运行所有测试(mvn clean test)时,它们将失败(它们依赖于新的上下文加载)。关键是我需要分别为每个测试类加载上下文。我想知道是否有办法做到这一点。
在我看到的日志文件中(仅一次):
Loading XML bean definitions from class path resource [context/mycontext.xml]
//..other contexts are loaded
编辑:我正在使用 Spring 3.2.8.RELEASE、gmavenplus-plugin(版本 1.5)和 spockframework。
最佳答案
使用@DirtiesContext
例如:
@ContextConfiguration(locations="classpath:context/mycontext.xml")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
class SampleSpec extends Specification {
关于java - Spring-Spock 为多个测试加载一次上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32625147/