我在Eclipse(霓虹灯)中创建的Android测试项目中使用PowerMock。当我运行基本测试用例时,我在调用PowerMockito.spy()
时收到 ExceptionInInitializationError 。
以下是报告的堆栈跟踪:
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
我的AndroidTestCase类:
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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
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>
最后我在项目libs文件夹中包含的.jar库(在构建路径中):
除了dexmaker .jar库之外,所有其他库.jars都是从{{3}下载的 powermock-mockito-junit-1.6.2.zip 存档中获取的。 }。 dexmaker .jar库是从PowerMock GitHub project获得的。
为了让库彼此很好地编译,我必须修改并重新打包 mockito-all-1.10.19.jar 和 powermock-mockito-1.6.2- full.jar 库,因为它们与其他.jar库存在以下冲突:
此外, powermock-mockito-junit-1.6.2.zip 存档中包含的整个 objenesis-2.1.jar 库存在冲突,因为其内容已经包含在 mockito-all-1.10.19.jar 库中,所以我没有在测试项目中包含该库。
在模拟器(Nexus 5,Google API,API 21)和设备(Nexus 5,Android 5.1.1)上尝试了测试用例。
这几乎描述了所有项目设置和来源。我已经尝试在互联网上挖掘一段时间,但我仍然不知道我的设置中有什么错误或遗漏,所以任何帮助都会非常感激。提前谢谢!
答案 0 :(得分:0)
您收到此错误是因为您必须将实例传递给PowerMockito.spy方法。它应该是这样的:
PowerMockito.spy(new MyStaticClass());