java.lang.ExceptionInInitializationError

152 views
Skip to first unread message

Jan-Paul Azucena

unread,
Sep 15, 2015, 11:13:47 PM9/15/15
to PowerMock
Hi,

I'm using PowerMock in an Android Test Project that I've created in Eclipse (Neon).  When I run a basic test case I am receiving an ExceptionInInitializationError when calling PowerMockito.spy().  Here is the stacktrace that's reported:

I/TestRunner(30132): started: testAndroidTestCaseSetupProperly(com.fuelpowered.lib.propeller.test.JPTestCase)
I/TestRunner(30132): finished: testAndroidTestCaseSetupProperly(com.fuelpowered.lib.propeller.test.JPTestCase)
I/TestRunner(30132): passed: testAndroidTestCaseSetupProperly(com.fuelpowered.lib.propeller.test.JPTestCase)
I/TestRunner(30132): started: testJP(com.fuelpowered.lib.propeller.test.JPTestCase)
I/ActivityManager(  757): Start proc 30167:com.spryfox.tripletown/u0a199 for broadcast com.spryfox.tripletown/com.heyzap.sdk.ads.PackageAddedReceiver
I/TestRunner(30132): failed: testJP(com.fuelpowered.lib.propeller.test.JPTestCase)
I/TestRunner(30132): ----- begin exception -----
I/TestRunner(30132): 
I/TestRunner(30132): java.lang.ExceptionInInitializerError
I/TestRunner(30132): at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)
I/TestRunner(30132): at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57)
I/TestRunner(30132): at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110)
I/TestRunner(30132): at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
I/TestRunner(30132): at org.powermock.api.mockito.PowerMockito.spy(PowerMockito.java:234)
I/TestRunner(30132): at com.fuelpowered.lib.propeller.test.JPTestCase.testJP(JPTestCase.java:18)
I/TestRunner(30132): at java.lang.reflect.Method.invoke(Native Method)
I/TestRunner(30132): at java.lang.reflect.Method.invoke(Method.java:372)
I/TestRunner(30132): at junit.framework.TestCase.runTest(TestCase.java:168)
I/TestRunner(30132): at junit.framework.TestCase.runBare(TestCase.java:134)
I/TestRunner(30132): at junit.framework.TestResult$1.protect(TestResult.java:115)
I/TestRunner(30132): at junit.framework.TestResult.runProtected(TestResult.java:133)
I/TestRunner(30132): at junit.framework.TestResult.run(TestResult.java:118)
I/TestRunner(30132): at junit.framework.TestCase.run(TestCase.java:124)
I/TestRunner(30132): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
I/TestRunner(30132): at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
I/TestRunner(30132): at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
I/TestRunner(30132): at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
I/TestRunner(30132): Caused by: org.mockito.cglib.core.CodeGenerationException: java.lang.reflect.InvocationTargetException-->null
I/TestRunner(30132): at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238)
I/TestRunner(30132): at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
I/TestRunner(30132): at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
I/TestRunner(30132): at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
I/TestRunner(30132): at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
I/TestRunner(30132): at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
I/TestRunner(30132): ... 18 more
I/TestRunner(30132): Caused by: java.lang.reflect.InvocationTargetException
I/TestRunner(30132): at java.lang.reflect.Method.invoke(Native Method)
I/TestRunner(30132): at java.lang.reflect.Method.invoke(Method.java:372)
I/TestRunner(30132): at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385)
I/TestRunner(30132): at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220)
I/TestRunner(30132): ... 23 more
I/TestRunner(30132): Caused by: java.lang.UnsupportedOperationException: can't load this type of class file
I/TestRunner(30132): at java.lang.ClassLoader.defineClass(ClassLoader.java:300)
I/TestRunner(30132): ... 27 more
I/TestRunner(30132): ----- end exception -----
I/TestRunner(30132): finished: testJP(com.fuelpowered.lib.propeller.test.JPTestCase)
D/AndroidRuntime(30094): Shutting down VM
I/ActivityManager(  757): Force stopping com.fuelpowered.lib.propeller.test appid=10324 user=0: finished inst
I/ActivityManager(  757): Killing 30132:com.fuelpowered.lib.propeller.test/u0a324 (adj 0): stop com.fuelpowered.lib.propeller.test 

My AndroidTestCase class:

package com.fuelpowered.lib.propeller.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import android.test.AndroidTestCase;

@RunWith(PowerMockRunner.class)
@PrepareForTest({
MyStaticClass.class
})
public class JPTestCase extends AndroidTestCase {

@Test
public void testJP() {
PowerMockito.spy(MyStaticClass.class);
assertTrue(true);
}

}

MyStaticClass.java:

package com.fuelpowered.lib.propeller.test;

public class MyStaticClass {

public static String foo() {
return bar();
}

public static String bar() {
return "bar";
}

}

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
    package="com.fuelpowered.lib.propeller.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="23" />

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:targetPackage="com.fuelpowered.lib.propeller.test" />

    <application
        android:allowBackup="false"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

And finally the .jar libraries that I have included (in the build path) in the project libs folder:
  • cglib-nodep-2.2.2.jar
  • dexmaker-1.4.jar
  • dexmaker-mockito-1.4.jar
  • javassist-3.19.0-GA.jar
  • junit-4.12.jar
  • mockito-all-1.10.19.jar
  • powermock-mockito-1.6.2-full.jar
With the exception of the dexmaker .jar libraries, all of the other library .jars were obtained from the powermock-mockito-junit-1.6.2.zip archive downloaded from the PowerMock GitHub project.  The dexmaker .jar libraries were obtained from the dexmaker GitHub project.

In order to get the libraries to compile nicely with one another I had to modify and repack the mockito-all-1.10.19.jar and powermock-mockito-1.6.2-full.jar libraries since they had the following conflicts with the other .jar libraries:
  • ./LICENSE, ./NOTICE, and ./asm-license.txt files in mockito-all-1.10.19.jar conflicted with the existence of files with the same names in cglib-nodep-2.2.2.jar, so they were renamed
  • ./mockito-extensions/org.mockito.plugins.MockMaker file powermock-mockito-1.6.1-full.jar conflicted with the existence of a file with the same name in dexmaker-mockito-1.4.jar, so the file was removed from the PowerMock .jar library.
Additionally, there was a conflict of the entire objenesis-2.1.jar library included in the powermock-mockito-junit-1.6.2.zip archive since its contents were already included within the mockito-all-1.10.19.jar library, so I did not include that library in the test project.

The test case was attempted on both the simulator (Nexus 5, Google APIs, API 21) and device (Nexus 5, Android 5.1.1).

This pretty much describes all of the project settings and sources.  I don't have any clue as to what is wrong or missing in my setup so any help would be greatly appreciated.  Thanks in advance!

Johan Haleby

unread,
Sep 16, 2015, 12:01:20 AM9/16/15
to powe...@googlegroups.com
Are you trying to run your tests on the JVM or on the device?

--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock.
For more options, visit https://groups.google.com/d/optout.

Jan-Paul Azucena

unread,
Sep 16, 2015, 12:55:54 PM9/16/15
to PowerMock
I am trying to run the tests on device/simulator.  I didn't think there was a way to run PowerMock/Mockito on the JVM (not without some other framework like Robolectric).

Johan Haleby

unread,
Sep 17, 2015, 2:23:04 AM9/17/15
to powe...@googlegroups.com
PowerMock/PowerMockito only works on the JVM, it will not work on the device or simulator.
Reply all
Reply to author
Forward
0 new messages