java - Spring-Spock 为多个测试加载一次上下文

标签 java spring maven groovy spock

我对 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/

相关文章:

java - Mybatis执行存储过程时无法设置OUT参数

java - 如何调试成 map ?

java - OnClick 用 fragment 替换 RecyclerView 中的 View

java - Springs MVC JSON 响应并将其转换为 JS 对象

java - 通过 Yaml 配置 H2 数据库 - Spring Boot

java - Android 媒体播放器在准备语句时出错

java - Spring Java + 将模板文件设置为样式表

java - Spring Batch + Spring API REST

java - JAXB maven 插件只能在发生变化时生成源代码吗?

java - 带有 Intellij 的 Maven WebApp - 程序