nickylin / powermock

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

Calling getClass() in a groovy test results in exception: "too much data" #235

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create a groovy test (extending GroovyTestCase)
2. Set the class up to use the @RunWith(PowerMockRunner.class)
@PrepareForTest([MyTest.class]) annotations
3. In a test method call Object.getClass()

What is the expected output? What do you see instead?
Output is:
java.lang.RuntimeException: javassist.CannotCompileException: by
java.io.IOException: too much data
    at
org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader
.java:188)
    at
org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader
.java:148)
    at
org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupport
ingClassLoader.java:63)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
    at
org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCa
llSite.java:199)
    at
com.forresthumphrey.bugz.powermock.ExampleWithoutIgnoreTest.testGetClassFails(Ex
ampleWithoutIgnoreTest.groovy:12)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$Po
werMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java
:322)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadi
e.java:94)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$Po
werMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:3
09)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$Po
werMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl
.java:112)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$Po
werMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:7
3)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$Po
werMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDe
legateImpl.java:297)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.in
vokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:222)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.ru
nMethods(PowerMockJUnit44RunnerDelegateImpl.java:161)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.
run(PowerMockJUnit44RunnerDelegateImpl.java:135)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.ru
n(PowerMockJUnit44RunnerDelegateImpl.java:133)
    at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run
(JUnit4TestSuiteChunkerImpl.java:112)
    at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.
run(AbstractCommonPowerMockRunner.java:55)
    at
org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
    at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(Abstra
ctDirectoryTestSuite.java:140)
    at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirec
toryTestSuite.java:127)
    at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.jav
a:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at
org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBoote
r.java:345)
    at
org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Caused by: javassist.CannotCompileException: by java.io.IOException: too
much data
    at javassist.CtClassType.toBytecode(CtClassType.java:1343)
    at javassist.CtClass.toBytecode(CtClass.java:1220)
    at
org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader
.java:185)
    ... 38 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:600)
    at javassist.CtClassType.toBytecode(CtClassType.java:1331)
    ... 40 more

What version of the product are you using? On what operating system?
Product: Powermock mockito api, 1.3.5
Mockito: 1.8.2
OS: Mac OS X, Snow Leopard, 10.6.2
Groovy: 1.6.5
JVM: 1.6.0_17

Please provide any additional information below.
I have attached a very simple test case. I have included tests for just the
getClass() call as well as getClass().getResource() because I would like to
see the fix for this bug also handle the getResource() call (possibly by
using the deferring ClassLoader)

Original issue reported on code.google.com by nerd4chr...@gmail.com on 16 Feb 2010 at 2:25

Attachments:

GoogleCodeExporter commented 9 years ago
I don't know Groovy but I suppose you would get the same exception in pure 
Java? I 
have so much to do right now so it would really help if I could get a java 
example 
instead. It's possible that it is a Javassist issue as well..

Original comment by johan.ha...@gmail.com on 16 Feb 2010 at 2:07

GoogleCodeExporter commented 9 years ago
You could try upgrading to Javassist 3.11 and see if you still get the same 
result.

Original comment by johan.ha...@gmail.com on 16 Feb 2010 at 2:40

GoogleCodeExporter commented 9 years ago
Hi Johan,

I don't believe the problem exists with a straight-java test. I have attached an
updated test case that includes a passing java test.

Please find in the attached two separate groovy tests, one
(GroovyPowermockWithoutIgnoreTest) that demonstrates the failure that results 
when
calling getClass() and the other (GroovyPowermockWithIgnoreTest) demonstrates 
that
using the following annotation causes the getClass() to work without problem:
@PowerMockIgnore(['org.codehaus.groovy', 'groovy.lang'])

I am unable to update the test case to depend on 3.11.0.GA of javassist because 
they
have not published that artifact to the public maven repository. As the pom is 
set up
now, it will just transitively grab whatever javassist version that the 
powermock
dependency declares.

Original comment by nerd4chr...@gmail.com on 17 Feb 2010 at 5:25

Attachments:

GoogleCodeExporter commented 9 years ago
I am encountering the same issue. Mockito does not appear to have any problems 
so I'm
curious as to if it might be related to the RunWith and PrepareForTest 
annotations.

Original comment by samueldo...@gmail.com on 23 Mar 2010 at 2:19

GoogleCodeExporter commented 9 years ago
One more thing, seeing how there is a growing tendency to write tests using 
Groovy
over Java this defect should probably be raised to a higher level of severity.

Original comment by samueldo...@gmail.com on 23 Mar 2010 at 2:21

GoogleCodeExporter commented 9 years ago
I agree, however I have very little experience with groovy myself so it would 
be 
really good if someone could help out tracking this down.

Original comment by johan.ha...@gmail.com on 23 Mar 2010 at 7:23

GoogleCodeExporter commented 9 years ago
It appears to be purely an issue with javassist being unable to serialize 
groovy class files to byte arrays.  Attached is a JUnit test proving it can be 
reproduced without having powermock in the mix.  This is using 
javassist-3.14.0-GA.

Original comment by mahoney...@gmail.com on 23 May 2011 at 9:20

Attachments:

GoogleCodeExporter commented 9 years ago
I have raised an issue on javassist here:
https://issues.jboss.org/browse/JASSIST-142

Original comment by mahoney...@gmail.com on 23 May 2011 at 9:37

GoogleCodeExporter commented 9 years ago
Thanks for your help!

Original comment by johan.ha...@gmail.com on 24 May 2011 at 6:32

GoogleCodeExporter commented 9 years ago
Manually specifying org.javassist:javassist:3.15.0-GA as the javassist version 
fixes this issue.

Original comment by mahoney...@gmail.com on 15 Jan 2012 at 9:12

GoogleCodeExporter commented 9 years ago
Thanks for letting us know.

Original comment by johan.ha...@gmail.com on 16 Jan 2012 at 8:48

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Which version of javaassist is compatible with latest powermock 1.5 ? I have 
used 3.15.0-GA and I am getting the below error : 

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:91)
    at com.csfb.primeview.workspace.WorkSpaceECPTest.<clinit>(WorkSpaceECPTest.java:39)
    at sun.reflect.GeneratedSerializationConstructorAccessor21.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40)
    at org.powermock.reflect.internal.WhiteboxImpl.newInstance(WhiteboxImpl.java:229)
    at org.powermock.reflect.Whitebox.newInstance(Whitebox.java:139)
    at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:123)
    at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
    at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
    at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
    at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
    at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
    at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
    at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
    at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
    at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
    at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
    at org.powermock.classloading.DeepCloner.cloneFields(DeepCloner.java:248)
    at org.powermock.classloading.DeepCloner.performClone(DeepCloner.java:128)
    at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:82)
    at org.powermock.classloading.DeepCloner.clone(DeepCloner.java:69)
    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.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Also, since there are so many jars involved with javaassist - it would be nice 
if all the versions of related library are listed at one single place. There is 
an initial attempt to do that - but, there are extensions like PowerMockRule. I 
have spent more than a complete day to integrate PowerMock with Mockito - but, 
I am still not able to see that green bar in Junit console. Oscillating from 
fixing one stack trace to other. So far, I have these powermock libraries in my 
classpath :

mockito-all-1.9.5-rc1.jar
objenesis-tck-1.3.jar
powermock-module-junit4-rule-1.5.jar
powermock-module-junit4-rule-agent-1.5.jar
powermock-module-javaagent-1.5.jar
powermock-mockito-1.5-full.jar
powermock-classloading-base-1.5.jar
powermock-classloading-objenesis-1.5.jar
javassist-3.15.0-GA.jar
powermock-api-support-1.5.jar

I am using Spring 3 and Junit 4.8.2. 

Request to anyone in Powermock community :

If a test case to test static method in Spring 3 ecosystem is working - with 
PowerMockAgent and PowerMockRule : please share the libraries and the 
corresponding version you are using.

Thanks,
Goverdhan

Original comment by Goverdha...@gmail.com on 16 May 2013 at 1:57

GoogleCodeExporter commented 9 years ago
PLease upgrade to the latest version of Javassist and see if you get the same 
error, 3.15 is really old.

Original comment by johan.ha...@gmail.com on 17 May 2013 at 4:57

GoogleCodeExporter commented 9 years ago
Upgraded to javassist-3.17.1-GA.jar .. still the same error. Thanks.

Original comment by Goverdha...@gmail.com on 20 May 2013 at 10:54

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
There are examples of using the both the classloader and agent based 
PowerMockRule in the example projects in the SVN repo. Don't think they use 
Spring 3 though, but not sure if Spring is really the problem.

Original comment by johan.ha...@gmail.com on 21 May 2013 at 9:25

GoogleCodeExporter commented 9 years ago
Johan - Thanks for the reply. The problem is my company does not allow me to 
connect connect to your SVN, perhaps for firewall security reasons. I have 
deleted all the  previous jars from my classpath and downloaded the dependency 
jars from 

http://code.google.com/p/powermock/downloads/detail?name=powermock-mockito-testn
g-1.5.zip&can=2&q=

Added them at the top of my .classpath file and And added -
powermock-module-junit4-rule-1.5.jar
powermock-module-junit4-rule-agent-1.5.jar
powermock-module-javaagent-1.5.jar
powermock-classloading-base-1.5.jar
powermock-classloading-objenesis-1.5.jar

Done the regular clean-build. Surprisingly, I continue to see the same error. I 
am running out of ideas. Please let me know if there is a ziped project with 
all the jars and sample code available. Thanks again.

Original comment by Goverdha...@gmail.com on 21 May 2013 at 2:41

GoogleCodeExporter commented 9 years ago
Can't you checkout the source when you're home?

Original comment by johan.ha...@gmail.com on 22 May 2013 at 6:27

GoogleCodeExporter commented 9 years ago
For now, I have given up on working with PowerMockAgent and PowerMockRule. They 
were required because I was running my test case with Spring i.e with 
@RunWith(SpringJUnit4ClassRunner.class)

I am able to get a standalone PowerMock testcase working : but only after I 
have removed PowerMockAgent/PowerMockRule and related jar files - and when I 
run my testcase with @RunWith(PowerMockRunner.class) which obviously means I 
can't use @RunWith(SpringJUnit4ClassRunner.class). 

But, seeing the standalone PowerMock testcase working was a good first step and 
I must thank Johan for sharing his wonderful work. Would look forward to see 
more updates around this.

Original comment by Goverdha...@gmail.com on 22 May 2013 at 11:20

GoogleCodeExporter commented 9 years ago
Thanks! Both rule implementations are experimental and some problems are bound 
to happen if the environment is complex. But I've always managed to resolve the 
issues I've had personally so far :)

Original comment by johan.ha...@gmail.com on 22 May 2013 at 11:46