我正在使用以下spock依赖
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.0-groovy-2.4</version>
<scope>test</scope>
</dependency>
我写了这个测试
class JsonIncludeResolverTest extends Specification {
JsonIncludeResolver uut;
def "test"(){
expect:
true
}
}
测试成功
现在我想使用Spring在规范中注入JsonIncludeResolver
。
JsonIncludeResolver
使用@Component
进行注释,并在运行应用程序时成功注入。测试和组件都放在同一个maven模块中。
我在这里阅读了spring-spock
:How to inject spring beans into spock test
因此我添加了这个依赖版本
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<version>1.1-groovy-2.4</version>
<scope>test</scope>
</dependency>
添加此依赖项后运行s
Exception in thread "main" java.lang.NoSuchMethodError: org.spockframework.util.ReflectionUtil.isAnnotationPresentRecursive(Ljava/lang/Class;Ljava/lang/Class;)Z
at org.spockframework.spring.SpringExtension.isSpringSpec(SpringExtension.java:83)
at org.spockframework.spring.SpringExtension.visitSpec(SpringExtension.java:59)
at org.spockframework.runtime.ExtensionRunner.runGlobalExtensions(ExtensionRunner.java:46)
at org.spockframework.runtime.ExtensionRunner.run(ExtensionRunner.java:40)
at org.spockframework.runtime.Sputnik.runExtensionsIfNecessary(Sputnik.java:88)
at org.spockframework.runtime.Sputnik.getDescription(Sputnik.java:55)
at com.intellij.junit4.JUnit4IdeaTestRunner.getDescription(JUnit4IdeaTestRunner.java:78)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:50)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
然后我尝试使用正确的注释
@ContextConfiguration
class JsonIncludeResolverTest extends Specification {
@Autowired
JsonIncludeResolver uut;
def "test"(){
expect:
uut != null
}
}
结果仍然是上述例外。
groovy测试在/src/test/groovy
下,这是在第一次成功运行测试时可以看到的。
答案 0 :(得分:0)
尝试按如下方式将Spock升级到1.1:
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.1-groovy-2.4</version>
<scope>test</scope>
</dependency>
自this commit版本1.1以来已经引入了{p> org.spockframework.util.ReflectionUtil.isAnnotationPresentRecursive()
。