Spock: Mock inner class fail

Created on 21 Jul 2016  路  4Comments  路  Source: spockframework/spock

Issue description

If try to mock a inner class that override a super method which has a generic return type, the mock will fail.
Not sure it is Spock issue or cglib issue.

How to reproduce

The code like below:

public abstract class Builder<T> {
    protected abstract T createInstance();
}

public class AnnotationMeta {
    public static class Builder1 extends Builder<String> {
        @Override
        protected String createInstance() {
            return null;
        }
    }
}

Then create mock for AnnotationMeta.Build1 below:

def mockTest = Mock(AnnotationMeta.Builder1)

The exception like below:

java.lang.IllegalArgumentException
    at net.sf.cglib.proxy.BridgeMethodResolver.resolveAll(BridgeMethodResolver.java:61)
    at net.sf.cglib.proxy.Enhancer.emitMethods(Enhancer.java:911)
    at net.sf.cglib.proxy.Enhancer.generateClass(Enhancer.java:498)
    at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
    at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
    at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
    at net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
    at org.spockframework.mock.runtime.ProxyBasedMockFactory$CglibMockFactory.createMock(ProxyBasedMockFactory.java:91)
    at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:49)
    at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:51)
    at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
    at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:45)
    at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:281)
    at org.spockframework.lang.SpecInternals.MockImpl(SpecInternals.java:103)
    at uapi.annotation.MethodMetaTest.Test build(MethodMetaTest.groovy:23)

Link to a gist or similar (optional)

Additional Environment information

Version of your build tool(if used), Java, Groovy, IDE, OS etc

Java/JDK

java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b15)
Java HotSpot(TM) Client VM (build 25.91-b15, mixed mode, sharing)

Groovy version

2.4.1

Build tool version

Gradle

2.11

Apache Maven

N/A

Operating System

Windows

IDE

IntelliJ

Build-tool dependencies used

Gradle/Grails

testCompile ( "org.spockframework:spock-core:1.0-groovy-2.4")

Apache Maven

N/A

Groovy Grape

N/A

Scala SBT

N/A

Most helpful comment

I'm not able to reproduce your problem using the dependencies below:

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.1'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'cglib:cglib-nodep:3.2.4'
}

Could you please provide a SSCCE?

All 4 comments

I'm not able to reproduce your problem using the dependencies below:

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.4.1'
    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'
    testCompile 'cglib:cglib-nodep:3.2.4'
}

Could you please provide a SSCCE?

@siordache,
I have created a project to reproduce the issue: https://github.com/minjing/spock-test
You can clone and run "./gradlew clean build" that should be failed in the test phase, check the test report under "test1/build/reports/test" you will see the exception that I reported in the issue description.

Best Regards.

You currently use Java 8 and cglib 3.1. As mentioned in the documentation, Java 8 is only supported from CGLIB 3.2.0 onwards.

I sent a PR to your test project with the needed changes.

It works.

Thanks for your patience.

Was this page helpful?
0 / 5 - 0 ratings