Spock is being used to execute an integration test in a Spring Boot project (2.1.18.RELEASE). When I run with 1.3-groovy-2.5, I get this error:
Caused by: java.lang.RuntimeException: not supported: class org.spockframework.gentyref.CaptureTypeImpl
at org.spockframework.gentyref.GenericTypeReflector.erase(GenericTypeReflector.java:33)
at org.spockframework.mock.runtime.StaticMockMethod.getReturnType(StaticMockMethod.java:50)
at org.spockframework.mock.EmptyOrDummyResponse.respond(EmptyOrDummyResponse.java:68)
at org.spockframework.mock.runtime.MockController.handle(MockController.java:50)
at org.spockframework.mock.runtime.JavaMockInterceptor.intercept(JavaMockInterceptor.java:72)
at org.spockframework.mock.runtime.ByteBuddyInterceptorAdapter.interceptNonAbstract(ByteBuddyInterceptorAdapter.java:35)
at com.foo.controller.ConversionsController.createConversionJob(ConversionsController.java:68)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
... 18 more
If I update Spock to a more recent version (eg. 2.1-groovy-2.5) I get this error:
[ERROR] org.apache.maven.surefire.booter.SurefireBooterForkException: There was an error in the forked process
[ERROR] java.util.ServiceConfigurationError: org.junit.platform.engine.TestEngine: org.spockframework.runtime.SpockEngine Unable to get public no-arg constructor
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.fork(ForkStarter.java:733)
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:305)
[ERROR] at org.apache.maven.plugin.surefire.booterclient.ForkStarter.run(ForkStarter.java:265)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeProvider(AbstractSurefireMojo.java:1314)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.executeAfterPreconditionsChecked(AbstractSurefireMojo.java:1159)
[ERROR] at org.apache.maven.plugin.surefire.AbstractSurefireMojo.execute(AbstractSurefireMojo.java:932)
[ERROR] at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
[ERROR] at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
[ERROR] at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
[ERROR] at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957)
[ERROR] at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289)
[ERROR] at org.apache.maven.cli.MavenCli.main(MavenCli.java:193)
[ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR] at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.base/java.lang.reflect.Method.invoke(Method.java:566)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
I am using Java 11 and Maven 3.6.3. My pom.xml is rather long, so I've reduced it to some snippets that focus on versions and test dependencies:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>11</java.version>
<bytebuddy.version>1.11.0</bytebuddy.version>
<cglib.version>3.2.12</cglib.version>
<gmavenplus.version>1.12.0</gmavenplus.version>
<groovy.version>2.5.15</groovy.version>
<objenesis.version>3.2</objenesis.version>
<spock.version>1.3-groovy-2.5</spock.version>
<maven.buildhelper.version>3.3.0</maven.buildhelper.version>
<maven.jacoco.version>0.8.7</maven.jacoco.version>
<maven.surefire.version>3.0.0-M5</maven.surefire.version>
<maven.failsafe.version>3.0.0-M5</maven.failsafe.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<!-- uncommented for 2.1-groovy-2.5 -->
<!-- dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-junit4</artifactId>
<scope>test</scope>
</dependency -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-spring</artifactId>
<scope>test</scope>
</dependency>
<dependency> <!-- enables mocking of classes (in addition to interfaces) -->
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>${bytebuddy.version}</version>
<scope>test</scope>
</dependency>
<dependency> <!-- enables mocking of classes without default constructor (together with ByteBuddy or CGLIB) -->
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
<version>${objenesis.version}</version>
<scope>test</scope>
</dependency>
<plugin>
<!-- The gmavenplus plugin is used to compile Groovy code. To learn more about this plugin,
visit https://github.com/groovy/GMavenPlus/wiki -->
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>${gmavenplus.version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
<configuration>
<targetBytecode>11</targetBytecode>
</configuration>
</plugin>
Any help with troubleshooting this further is greatly appreciated. I modelled my pom.xml after the examples in the spock framework project:
- https://github.com/spockframework/spock-example/blob/master/pom.xml
- https://github.com/spockframework/spock-example/blob/spock-1.x/pom.xml
CodePudding user response:
Regarding java.util.ServiceConfigurationError: org.junit.platform.engine.TestEngine: org.spockframework.runtime.SpockEngine Unable to get public no-arg constructor
Spring Boot 2.1.18.RELEASE
is really old, it manages JUnit 5 to 5.3.2
while Spock 2.x requires >= 5.8
. You can try setting <junit-jupiter.version>5.8.1</junit-jupiter.version>
if you can't upgrade Spring Boot to a more recent version.
As for the type reflection error, we can't say much since you didn't share any code. Only that com.foo.controller.ConversionsController.createConversionJob(ConversionsController.java:68)
probably has some weird generics or is calling something that does.
CodePudding user response:
I accepted the answer from Leonard Brünings as it pointed me in the right direction, but wanted to add more explanation as to cause of the problem.
The java.lang.RuntimeException: not supported: class org.spockframework.gentyref.CaptureTypeImpl
is a reflection error that indicates a problem with a method being mocked.
I had a method like this:
public class Foo {
public String bar(Long id) {
...
}
}
In my test I did this:
setup:
Foo mockFoo = Stub()
given:
String id = '123'
and:
mockFoo.bar(id) >> 'abc'
...
Because id
was declared a String
instead of a Long
, the exception was thrown. However, when I changed it to a Mock()
instead of a Stub()
and also used a cardinality rule, then it became apparent that the mocked method was not being invoked with a more useful error message:
then:
mockFoo.bar(id) >> 'abc'
0 * _ // don't allow any other interaction
Handler dispatch failed; nested exception is Too many invocations for:
0 * _ // don't allow any other interaction (1 invocation)