EdwinLi / powermock

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

Problem creating mock using PowerMock #280

Closed GoogleCodeExporter closed 9 years ago

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

I am using following in my pom.xml

        <dependency> 
            <groupId>org.powermock.modules</groupId> 
            <artifactId>powermock-module-junit4</artifactId> 
            <version>1.4.5</version> 
            <scope>test</scope> 
        </dependency>
        <dependency>
          <groupId>com.google.collections</groupId>
          <artifactId>google-collections</artifactId>
          <version>1.0</version>
          <scope>test</scope>
    </dependency>   

        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymock</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.easymock</groupId>
            <artifactId>easymockclassextension</artifactId>
            <version>2.2</version>
            <scope>test</scope>
        </dependency>

I get this exception:

java.lang.IllegalStateException: Failed to transform class with name 
com.my.DroolsFlowManagerTest. Reason: 3
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:208)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:146)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:143)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:49)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:231)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:69)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:36)
    at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:27)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:29)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:40)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:30)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    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)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 3
    at javassist.bytecode.ByteArray.write16bit(ByteArray.java:40)
    at javassist.bytecode.StackMapTable$Shifter.update(StackMapTable.java:744)
    at javassist.bytecode.StackMapTable$Shifter.sameFrame(StackMapTable.java:720)
    at javassist.bytecode.StackMapTable$Walker.stackMapFrames(StackMapTable.java:194)
    at javassist.bytecode.StackMapTable$Walker.parse(StackMapTable.java:179)
    at javassist.bytecode.StackMapTable$Shifter.doit(StackMapTable.java:714)
    at javassist.bytecode.StackMapTable.shiftPc(StackMapTable.java:693)
    at javassist.bytecode.CodeIterator.insertGap0(CodeIterator.java:676)
    at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:636)
    at javassist.bytecode.CodeIterator.insertGapCore(CodeIterator.java:467)
    at javassist.bytecode.CodeIterator.insertGap(CodeIterator.java:413)
    at javassist.expr.Expr.replace0(Expr.java:298)
    at javassist.expr.FieldAccess.replace(FieldAccess.java:213)
    at org.powermock.core.transformers.impl.MainMockTransformer$PowerMockExpressionEditor.edit(MainMockTransformer.java:233)
    at javassist.expr.ExprEditor.loopBody(ExprEditor.java:197)
    at javassist.expr.ExprEditor.doit(ExprEditor.java:90)
    at javassist.CtClassType.instrument(CtClassType.java:1289)
    at org.powermock.core.transformers.impl.MainMockTransformer.transform(MainMockTransformer.java:70)
    at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:204)
    ... 30 more

then i added javassist to my pom.xml like this:
        <dependency>
          <groupId>javassist</groupId>
          <artifactId>javassist</artifactId>
          <version>3.10.0.GA</version>
          <scope>test</scope>
        </dependency>

I've got rid of that problem but now I get another exception while trying to 
use PowerMock.createMock() method. Here is my code to create mock. its simple:

createMock(ByteArrayInputStream.class);

Exception i get is:
java.lang.IllegalArgumentException: java.io.ByteArrayInputStream is not an 
interface
    at java.lang.reflect.Proxy.getProxyClass(Proxy.java:362)
    at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:581)
    at org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:12)
    at org.easymock.internal.MocksControl.createMock(MocksControl.java:37)
    at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2211)
    at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2162)
    at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:98)
    at net.plus.kbd.core.drools.DroolsFlowManagerTest.testReverseFlow(DroolsFlowManagerTest.java:103)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: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$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:322)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:309)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.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.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:222)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(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.run(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:57)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    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)

What is the expected output? What do you see instead?
Exception should not be thrown and my tests should work as expected.

What version of the product are you using? On what operating system?
I am using version 1.4.5 on debian lenny.

Please provide any additional information below.

Original issue reported on code.google.com by ago...@gmail.com on 22 Sep 2010 at 11:23

GoogleCodeExporter commented 9 years ago
This looks like a problem in Javassist that PowerMock depends on to do 
byte-code manipulation. You should probably report it to javassist instead so 
that they can fix it.

Original comment by johan.ha...@gmail.com on 22 Sep 2010 at 1:12

GoogleCodeExporter commented 9 years ago
Thanks for reply. Does this mean PowerMock.createMock never works because of 
this javassist problem? Or if it works can you please let me know the 
compatible versions what version of PowerMock and what version of javassist 
will work together?

Thanks

Original comment by ago...@gmail.com on 22 Sep 2010 at 2:37

GoogleCodeExporter commented 9 years ago
It may never work for your specific use-case unless javassist fixes the bug :( 
PowerMock 1.4.5 is compatible with javassist 3.13.0.GA and probably 3.12.0.GA 
as well. It would be really good if you reported it to javassist. Perhaps 
PowerMock can create a work-around in the way we use javassist.

Original comment by johan.ha...@gmail.com on 22 Sep 2010 at 4:27

GoogleCodeExporter commented 9 years ago
Thanks for your reply. I've tried using javassist's version 3.12.0.GA and 
3.13.0.GA but still the exception is same:

java.lang.IllegalArgumentException: java.io.ByteArrayInputStream is not an 
interface
    at java.lang.reflect.Proxy.getProxyClass(Proxy.java:362)
    at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:581)
    at org.easymock.internal.JavaProxyFactory.createProxy(JavaProxyFactory.java:12)
    at org.easymock.internal.MocksControl.createMock(MocksControl.java:37)
    at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2211)
    at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2162)
    at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:98)
    at net.plus.kbd.core.drools.DroolsFlowManagerTest.testReverseFlow(DroolsFlowManagerTest.java:102)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java: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$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:322)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:309)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:112)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:73)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.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.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:222)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(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.run(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:57)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
    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)

I was wondering, does PowerMock automatically uses the EasyMock's class 
extensions? Or is there a way to specify that. I've looked at the examples on 
the website but it doesn't say anything about that. It just says to mock any 
classes with final methods etc just use PowerMock.createMock. But it looks 
appears to me that PowerMock is not using class extensions. Any idea?

Thank you.

Original comment by ago...@gmail.com on 23 Sep 2010 at 9:20

GoogleCodeExporter commented 9 years ago
You need to use version 3 of EasyMock if you're using version 1.4.5 of 
PowerMock! The classextensions module of EasyMock is deprecated and can now by 
default mock classes as well.

Original comment by johan.ha...@gmail.com on 23 Sep 2010 at 9:36

GoogleCodeExporter commented 9 years ago
After using version 3 of EasyMock, the problem is gone. Thank you for your help.

Original comment by ago...@gmail.com on 23 Sep 2010 at 12:46

GoogleCodeExporter commented 9 years ago
Thanks for the Information, really helped.

Original comment by rajeev.i...@gmail.com on 25 Oct 2011 at 5:48