vijayjcp / powermock

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

Verify if the mockStatic may cause ClassNotFoundException #208

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
User report:

StaticClass.java:
package com.anoop.test;

public class StaticClass {

       public static String getName(){
               return "Anoop";
       }

       public static void setName(String name){
               System.out.println(name);
       }
}

AnoopClass.java:
package com.anoop.test;

public class AnoopClass {

       private String name;
       public void doSomething(){
               name = StaticClass.getName();
               name = name + " does something";
       }

       public String getName(){
               return name;
       }
}

The Junit class:

package com.anoop.test;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.powermock.api.easymock.PowerMock.*;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.easymock.EasyMock.isNull;

@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticClass.class})
public class AnoopClassTest {

       private AnoopClass anoopClass;

       @Before
       public void setUp() throws Exception {
               anoopClass = new AnoopClass();
       }

       @Test
       public void testDoSomething() {
               mockStatic(StaticClass.class);
               expect(StaticClass.getName()).andReturn("Deepa");
               replay(StaticClass.class);
               anoopClass.doSomething();
               System.out.println(anoopClass.getName());
               verify(StaticClass.class);
       }

}

Error trace:
java.lang.NoClassDefFoundError: com.anoop.test.StaticClass$
$EnhancerByCGLIB$$3caf6adf
       at sun.reflect.GeneratedSerializationConstructorAccessor6.newInstance
(Unknown Source)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:521)
       at
org.easymock.classextension.internal.objenesis.instantiator.sun.SunReflectionFac
toryInstantiator.newInstance
(SunReflectionFactoryInstantiator.java:40)
       at
org.easymock.classextension.internal.objenesis.ObjenesisBase.newInstance
(ObjenesisBase.java:58)
       at
org.easymock.classextension.internal.objenesis.ObjenesisHelper.newInstance
(ObjenesisHelper.java:28)
       at
org.easymock.classextension.internal.ObjenesisClassInstantiator.newInstance
(ObjenesisClassInstantiator.java:12)
       at
org.powermock.api.easymock.internal.signedsupport.SignedSupportingClassProxyFact
ory.createProxy
(SignedSupportingClassProxyFactory.java:201)
       at org.easymock.internal.MocksControl.createMock(MocksControl.java:
40)
       at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:
2155)
       at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2110)
       at org.powermock.api.easymock.PowerMock.mockStatic(PowerMock.java:
290)
       at com.anoop.test.AnoopClassTest.testDoSomething(AnoopClassTest.java:
28)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:64)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:615)
       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:73)
       at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTest
(PowerMockJUnit47RunnerDelegateImpl.java:53)
       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.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:44)
       at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:38)
       at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196)

Original issue reported on code.google.com by johan.ha...@gmail.com on 26 Nov 2009 at 7:06

GoogleCodeExporter commented 8 years ago
This may be a JVM issue, GeoffH also reports:

I've seen something similar, but ONLY under the following conditions
a) running at a Windows command prompt and launching with Maven 2
b) JAVA_HOME set to C:\Program Files (x86)\IBM\SDP70\jdk

I then get a stack trace that looks similar (although I'm using
Mockito)

-------------------------------------------------------------------------------
Test set: my.pkg.FinalCLassMockTest
-------------------------------------------------------------------------------
Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.234
sec <<< FAILURE!
testCreateMockDummy(my.pkg.FinalCLassMockTest)  Time elapsed: 0.157
sec  <<< ERROR!
java.lang.NoClassDefFoundError: my.pkg.DummyClass$
$EnhancerByMockitoWithCGLIB$$b85681c3
       at sun.reflect.GeneratedSerializationConstructorAccessor13.newInstance
(Unknown Source)
       at java.lang.reflect.Constructor.newInstance(Constructor.java:521)
       at
org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance
(SunReflectionFactoryInstantiator.java:40)
       at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)
       at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy
(ClassImposterizer.java:120)
       at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise
(ClassImposterizer.java:60)
       at
org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocati
onControl
(MockCreator.java:79)
       at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock
(MockCreator.java:53)
       at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:80)
       at my.pkg.FinalCLassMock.createMockDummyClass(FinalCLassMock.java:56)

F:\Download\CodeSamples\MinimalPowerMockTest>java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build
pwi32dev-20061002a (SR3))
IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 Windows XP x86-32
j9vmwi3223-20061001 (JIT enabled)
J9VM - 20060915_08260_lHdSMR
JIT  - 20060908_1811_r8
GC   - 20060906_AA)
JCL  - 20061002

It also fails in Eclipse when the JRE is
C:\Program Files (x86)\IBM\SDP70\jdk\jre

I don't get this when I use:
a) JAVA_HOME=C:\Program Files (x86)\Java\jdk1.5.0_21
b) on unix under AIX

java -version
java version "1.5.0"
Java(TM) 2 Runtime Environment, Standard Edition (build
pap64devifx-20090225 (SR9-0 +IZ44410+IZ44495))
IBM J9 VM (build 2.3, J2RE 1.5.0 IBM J9 2.3 AIX ppc64-64
j9vmap6423ifx-20090225 (JIT enabled)
J9VM - 20090224_30451_BHdSMr
JIT  - 20081112_1511ifx1_r8
GC   - 200811_07)
JCL  - 20081129

Original comment by johan.ha...@gmail.com on 27 Nov 2009 at 10:50

GoogleCodeExporter commented 8 years ago
Both stacktraces seems to be related to objenesis. We could try to upgrade to 
the
latest version (1.2) or try Spring's version of objenesis.

Original comment by johan.ha...@gmail.com on 27 Nov 2009 at 10:51

GoogleCodeExporter commented 8 years ago
Objenesis doesn't seem to be the problem. From GeoffH:

F:\Tools\Objenesis>java -jar objenesis-tck-1.2.jar
Running TCK on platform: Java 1.5 (IBM Corporation IBM J9 VM 2.3
pwi32dev-20061002a (SR3))

Not serializable parent constructor called: Y

                                                   Objenesis
serializer Objenesis std
Constructor throwing exception                      N/
A                  Y
Constructor throwing exception (serializable)
Y                    Y
Constructor with arguments                          N/
A                  Y
Constructor with arguments (serializable)
Y                    Y
Constructor with mandatory arguments                N/
A                  Y
Constructor with mandatory arguments (serializable)
Y                    Y
Default package constructor                         N/
A                  Y
Default package constructor (serializable)
Y                    Y
Default private constructor                         N/
A                  Y
Default private constructor (serializable)
Y                    Y
Default protected constructor                       N/
A                  Y
Default protected constructor (serializable)
Y                    Y
Default public constructor                          N/
A                  Y
Default public constructor (serializable)
Y                    Y
No constructor                                      N/
A                  Y
No constructor (serializable)
Y                    Y
Serializable replacing with another class
Y                    Y
Serializable resolving to another class
Y                    Y
Serializable with ancestor throwing exception       N/
A                  Y

--- SUCCESSFUL: TCK tests passed without errors in 47 ms

I also updated my pom.xml to Objenesis version 1.2 and launched via
Maven 2 using IBM J9 JVM
 - I still got the same stack trace

Original comment by johan.ha...@gmail.com on 27 Nov 2009 at 3:09

GoogleCodeExporter commented 8 years ago
We had a similar problem with EasyMock and CGLib and Maven and we ended up 
creating a
really ugly hack to get around it in our MockClassloader. Perhaps we can try 
doing
something similar with this until we find time to find the real cause.

Original comment by johan.ha...@gmail.com on 27 Nov 2009 at 3:10

GoogleCodeExporter commented 8 years ago
Tried out the ugly fix and GeoffH reported that it didn't solve the issue. Need 
more
input in order to fix this.

Original comment by johan.ha...@gmail.com on 9 Dec 2009 at 9:46

GoogleCodeExporter commented 8 years ago
I am getting the same error, but in a different method:
Caused by: java.lang.NoClassDefFoundError: org/objenesis/Objenesis
    at
org.powermock.api.easymock.PowerMock.createPartialMockForAllMethodsExcept(PowerM
ock.java:365)

Original comment by pej...@gmail.com on 15 Apr 2010 at 1:11

GoogleCodeExporter commented 8 years ago
Aren't you just missing the Objenesis 1.2 jar in the classpath?

Original comment by johan.ha...@gmail.com on 16 Apr 2010 at 5:52

GoogleCodeExporter commented 8 years ago
No input from user in a long time.

Original comment by johan.ha...@gmail.com on 22 Jul 2010 at 9:25

GoogleCodeExporter commented 8 years ago

I get this stacktrace when I run one of my unit tests on only one of two 
computers. No problem on the other one. 

Both are running Ubuntu Linux with the exact same JDK version (1.6.0_19). The 
classpath is exactly the same. The only difference I have found is that the 
computer with the error has a 64-bit processor, whereas the other one is 32 
bit. 

Would be nice if someone else could prove or discard this theory.

Original comment by marterl...@gmail.com on 10 Aug 2010 at 12:38

GoogleCodeExporter commented 8 years ago
Yeah it would.. Really hard to tell what's causing this.

Original comment by johan.ha...@gmail.com on 13 Aug 2010 at 1:25

GoogleCodeExporter commented 8 years ago

Original comment by johan.ha...@gmail.com on 10 Apr 2011 at 12:52

GoogleCodeExporter commented 8 years ago
I got the same error using IBM Rational Application Developer (RAD).

By default the workspace uses the WebSphere JRE runtime. Once I pointed it to 
the IBM JDK everything worked as expected.

Original comment by s...@nanosn.com on 15 Dec 2011 at 12:10