【发布时间】:2018-11-15 17:14:45
【问题描述】:
我正在使用以下 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
}
}
结果还是上面的异常。
常规测试在/src/test/groovy 下,正如您在第一次成功运行测试时看到的那样。
【问题讨论】:
-
NoSuchMethodError表示jar版本不兼容 -
你会推荐我使用旧版本的
spring-spock吗? -
我建议使用兼容版本的 spring 和 spock-spring
-
如何计算出这种兼容性?
-
查看 spock-Version 的 maven repo,您将看到与 spring mvnrepository.com/artifact/org.spockframework/spock-spring/…的兼容性