Thoppan / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

Using PowerMock with Spock produces "Wrong return type in function" #326

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

  1. Attempting to mock native "final" java class using PowerMockAgent using Spock Framework (Groovy based).
  2. Add Spock framework to project.
  3. Create simple Spock feature test that references "final" Java class without using mocking and ensure the test passes and that Spock tests are working without PowerMock (http://code.google.com/p/spock/)
  4. Change test to use PowerMock (Mockito API) to mock final class (via PowerMockAgent) and repeat.

  See attached example, which contains a simple JUnit test that successfully mocks the final class using PowerMock, with the equivalent Spock test, which fails.

What is the expected output? What do you see instead?

Expected output is test passing; actual output is following exception being 
thrown by PowerMock:

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java 
-Dfile.encoding=UTF-8 -classpath /Applications/IntelliJ IDEA 
10.app/lib/idea_rt.jar:/Applications/IntelliJ IDEA 
10.app/plugins/junit/lib/junit-rt.jar:/System/Library/Java/Support/Deploy.bundle
/Contents/Resources/Java/deploy.jar:/System/Library/Java/JavaVirtualMachines/1.6
.0.jdk/Contents/Home/lib/dt.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.j
dk/Contents/Home/lib/javaws.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.j
dk/Contents/Home/lib/jce.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/
Contents/Home/lib/jconsole.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jd
k/Contents/Home/lib/management-agent.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Home/lib/plugin.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Home/lib/sa-jdi.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Classes/alt-rt.jar:/System/Library/Java/JavaVirtualMachines
/1.6.0.jdk/Contents/Classes/charsets.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Classes/classes.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Classes/jsse.jar:/System/Library/Java/JavaVirtualMachines/1
.6.0.jdk/Contents/Classes/ui.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.
jdk/Contents/Home/lib/ext/apple_provider.jar:/System/Library/Java/JavaVirtualMac
hines/1.6.0.jdk/Contents/Home/lib/ext/dnsns.jar:/System/Library/Java/JavaVirtual
Machines/1.6.0.jdk/Contents/Home/lib/ext/localedata.jar:/System/Library/Java/Jav
aVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunjce_provider.jar:/System/Lib
rary/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunpkcs11.jar:/Use
rs/mickeymouse/IdeaProjects/powermock-test1/out/test/powermock-test1:/Users/mick
eymouse/IdeaProjects/powermock-test1/out/production/powermock-test1:/Users/micke
ymouse/IdeaProjects/powermock-test1/lib/junit-4.8.2.jar:/Users/mickeymouse/IdeaP
rojects/powermock-test1/lib/mockito-all-1.8.5.jar:/Users/mickeymouse/IdeaProject
s/powermock-test1/lib/powermock-module-junit4-1.4.9.jar:/Users/mickeymouse/IdeaP
rojects/powermock-test1/lib/powermock-module-junit4-common-1.4.9.jar:/Users/mick
eymouse/IdeaProjects/powermock-test1/lib/powermock-core-1.4.9.jar:/Users/mickeym
ouse/IdeaProjects/powermock-test1/lib/powermock-reflect-1.4.9.jar:/Users/mickeym
ouse/IdeaProjects/powermock-test1/lib/objenesis-1.2.jar:/Users/mickeymouse/IdeaP
rojects/powermock-test1/lib/javassist-3.14.0-GA.jar:/Users/mickeymouse/IdeaProje
cts/powermock-test1/lib/powermock-api-mockito-1.4.9.jar:/Users/mickeymouse/IdeaP
rojects/powermock-test1/lib/junit-4.4.jar:/Users/mickeymouse/IdeaProjects/powerm
ock-test1/lib/powermock-api-support-1.4.9.jar:/Users/mickeymouse/IdeaProjects/po
wermock-test1/lib/powermock-module-junit4-rule-1.4.9.jar:/Users/mickeymouse/Idea
Projects/powermock-test1/lib/powermock-classloading-base-1.4.9.jar:/Users/mickey
mouse/IdeaProjects/powermock-test1/lib/powermock-tests-utils-1.4.9.jar:/Users/mi
ckeymouse/IdeaProjects/powermock-test1/lib/servlet-api-2.5.jar:/Users/mickeymous
e/IdeaProjects/powermock-test1/lib/powermock-classloading-xstream-1.4.9.jar:/Use
rs/mickeymouse/IdeaProjects/powermock-test1/lib/xstream-1.3.1.jar:/Users/mickeym
ouse/IdeaProjects/powermock-test1/lib/xpp3_min-1.1.4c.jar:/Users/mickeymouse/Ide
aProjects/powermock-test1/lib/powermock-module-junit4-rule-agent-1.4.9.jar:/User
s/mickeymouse/IdeaProjects/powermock-test1/lib/powermock-module-javaagent-1.4.9.
jar:/Users/mickeymouse/IdeaProjects/powermock-test1/lib/spock-core-0.5-groovy-1.
8.jar:/Users/mickeymouse/IdeaProjects/powermock-test1/lib/groovy-all-1.8.0-beta-
3-SNAPSHOT.jar:/Users/mickeymouse/IdeaProjects/powermock-test1/lib/junit-dep-4.8
.2.jar:/Users/mickeymouse/IdeaProjects/powermock-test1/lib/hamcrest-core-1.2.jar
 com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 
uk.co.radialsoftware.powermock.SimpleClassSpec

java.lang.VerifyError: (class: org/powermock/modules/agent/JDK6AgentLoader, 
method: getVirtualMachineImplementationFromEmbeddedOnes signature: 
()Lcom/sun/tools/attach/VirtualMachine;) Wrong return type in function
    at org.powermock.modules.agent.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:40)
    at org.powermock.modules.agent.PowerMockAgent.initializeIfNeeded(PowerMockAgent.java:83)
    at uk.co.radialsoftware.powermock.SimpleClassSpec.<clinit>(SimpleClassSpec.groovy:17)
    at com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider.newInstance(Sun14ReflectionProvider.java:74)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.instantiateNewInstance(AbstractReflectionConverter.java:308)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:161)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshallField(AbstractReflectionConverter.java:246)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doUnmarshal(AbstractReflectionConverter.java:218)
    at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.unmarshal(AbstractReflectionConverter.java:162)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java:82)
    at com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(AbstractReferenceUnmarshaller.java:63)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:76)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshaller.java:60)
    at com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:137)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(AbstractTreeMarshallingStrategy.java:33)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:923)
    at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:909)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:853)
    at com.thoughtworks.xstream.XStream.fromXML(XStream.java:845)
    at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:54)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:89)
    at org.powermock.classloading.ClassloaderExecutor.execute(ClassloaderExecutor.java:78)
    at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:49)
    at org.spockframework.runtime.extension.builtin.RuleInterceptor.intercept(RuleInterceptor.java:42)
    at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:67)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:60)

  Process finished with exit code 255

What version of the product are you using? On what operating system?

  PowerMock 1.4.9
  Spock 0.5-groovy-1.8

  IntelliJ IDEA 10.0.3
  Mac OSX 10.5 (Leopard)

Please provide any additional information below.

  Libraries referenced (via Maven, downloaded by IntelliJ - removed from attached example to keep size down):

    groovy-all-1.8.0-beta-3-SNAPSHOT.jar
    hamcrest-core-1.2.jar
    javassist-3.14.0-GA.jar
    junit-4.4.jar
    junit-4.8.2.jar
    junit-dep-4.8.2.jar
    mockito-all-1.8.5.jar
    objenesis-1.2.jar
    powermock-api-mockito-1.4.9.jar
    powermock-api-support-1.4.9.jar
    powermock-classloading-base-1.4.9.jar
    powermock-classloading-xstream-1.4.9.jar
    powermock-core-1.4.9.jar
    powermock-module-javaagent-1.4.9.jar
    powermock-module-junit4-1.4.9.jar
    powermock-module-junit4-common-1.4.9.jar
    powermock-module-junit4-rule-1.4.9.jar
    powermock-module-junit4-rule-agent-1.4.9.jar
    powermock-reflect-1.4.9.jar
    powermock-tests-utils-1.4.9.jar
    servlet-api-2.5.jar
    spock-core-0.5-groovy-1.8.jar
    xpp3_min-1.1.4c.jar
    xstream-1.3.1.jar

  Attempting to run without using PowerMockAgent.initializeIfNeeded() throws following:

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/bin/java 
-Dfile.encoding=UTF-8 -classpath /Applications/IntelliJ IDEA 
10.app/lib/idea_rt.jar:/Applications/IntelliJ IDEA 
10.app/plugins/junit/lib/junit-rt.jar:/System/Library/Java/Support/Deploy.bundle
/Contents/Resources/Java/deploy.jar:/System/Library/Java/JavaVirtualMachines/1.6
.0.jdk/Contents/Home/lib/dt.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.j
dk/Contents/Home/lib/javaws.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.j
dk/Contents/Home/lib/jce.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/
Contents/Home/lib/jconsole.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.jd
k/Contents/Home/lib/management-agent.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Home/lib/plugin.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Home/lib/sa-jdi.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Classes/alt-rt.jar:/System/Library/Java/JavaVirtualMachines
/1.6.0.jdk/Contents/Classes/charsets.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Classes/classes.jar:/System/Library/Java/JavaVirtualMachine
s/1.6.0.jdk/Contents/Classes/jsse.jar:/System/Library/Java/JavaVirtualMachines/1
.6.0.jdk/Contents/Classes/ui.jar:/System/Library/Java/JavaVirtualMachines/1.6.0.
jdk/Contents/Home/lib/ext/apple_provider.jar:/System/Library/Java/JavaVirtualMac
hines/1.6.0.jdk/Contents/Home/lib/ext/dnsns.jar:/System/Library/Java/JavaVirtual
Machines/1.6.0.jdk/Contents/Home/lib/ext/localedata.jar:/System/Library/Java/Jav
aVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunjce_provider.jar:/System/Lib
rary/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/lib/ext/sunpkcs11.jar:/Use
rs/mickeymouse/IdeaProjects/powermock-test1/out/test/powermock-test1:/Users/mick
eymouse/IdeaProjects/powermock-test1/out/production/powermock-test1:/Users/micke
ymouse/IdeaProjects/powermock-test1/lib/junit-4.8.2.jar:/Users/mickeymouse/IdeaP
rojects/powermock-test1/lib/mockito-all-1.8.5.jar:/Users/mickeymouse/IdeaProject
s/powermock-test1/lib/powermock-module-junit4-1.4.9.jar:/Users/mickeymouse/IdeaP
rojects/powermock-test1/lib/powermock-module-junit4-common-1.4.9.jar:/Users/mick
eymouse/IdeaProjects/powermock-test1/lib/powermock-core-1.4.9.jar:/Users/mickeym
ouse/IdeaProjects/powermock-test1/lib/powermock-reflect-1.4.9.jar:/Users/mickeym
ouse/IdeaProjects/powermock-test1/lib/objenesis-1.2.jar:/Users/mickeymouse/IdeaP
rojects/powermock-test1/lib/javassist-3.14.0-GA.jar:/Users/mickeymouse/IdeaProje
cts/powermock-test1/lib/powermock-api-mockito-1.4.9.jar:/Users/mickeymouse/IdeaP
rojects/powermock-test1/lib/junit-4.4.jar:/Users/mickeymouse/IdeaProjects/powerm
ock-test1/lib/powermock-api-support-1.4.9.jar:/Users/mickeymouse/IdeaProjects/po
wermock-test1/lib/powermock-module-junit4-rule-1.4.9.jar:/Users/mickeymouse/Idea
Projects/powermock-test1/lib/powermock-classloading-base-1.4.9.jar:/Users/mickey
mouse/IdeaProjects/powermock-test1/lib/powermock-tests-utils-1.4.9.jar:/Users/mi
ckeymouse/IdeaProjects/powermock-test1/lib/servlet-api-2.5.jar:/Users/mickeymous
e/IdeaProjects/powermock-test1/lib/powermock-classloading-xstream-1.4.9.jar:/Use
rs/mickeymouse/IdeaProjects/powermock-test1/lib/xstream-1.3.1.jar:/Users/mickeym
ouse/IdeaProjects/powermock-test1/lib/xpp3_min-1.1.4c.jar:/Users/mickeymouse/Ide
aProjects/powermock-test1/lib/powermock-module-junit4-rule-agent-1.4.9.jar:/User
s/mickeymouse/IdeaProjects/powermock-test1/lib/powermock-module-javaagent-1.4.9.
jar:/Users/mickeymouse/IdeaProjects/powermock-test1/lib/spock-core-0.5-groovy-1.
8.jar:/Users/mickeymouse/IdeaProjects/powermock-test1/lib/groovy-all-1.8.0-beta-
3-SNAPSHOT.jar:/Users/mickeymouse/IdeaProjects/powermock-test1/lib/junit-dep-4.8
.2.jar:/Users/mickeymouse/IdeaProjects/powermock-test1/lib/hamcrest-core-1.2.jar
 com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 
uk.co.radialsoftware.powermock.SimpleClassSpec

java.lang.RuntimeException: javassist.CannotCompileException: by 
java.io.IOException: too much data
    at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:187)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:147)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
    at uk.co.radialsoftware.powermock.SimpleClassSpec.simple class returns value from simple final class collaborator(SimpleClassSpec.groovy:23)
Caused by: javassist.CannotCompileException: by java.io.IOException: too much 
data
    at javassist.CtClassType.toBytecode(CtClassType.java:1438)
    at javassist.CtClass.toBytecode(CtClass.java:1279)
    at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java:184)
    ... 4 more
Caused by: java.io.IOException: too much data
    at javassist.ClassPoolTail.copyStream(ClassPoolTail.java:440)
    at javassist.ClassPoolTail.writeClassfile(ClassPoolTail.java:290)
    at javassist.ClassPool.writeClassfile(ClassPool.java:637)
    at javassist.CtClassType.toBytecode(CtClassType.java:1426)
    ... 6 more

  The above, may be related to issue 235:

    http://code.google.com/p/powermock/issues/detail?id=235

Original issue reported on code.google.com by dsommerv...@gmail.com on 5 May 2011 at 8:23

Attachments: