【发布时间】:2015-12-12 11:38:22
【问题描述】:
我在 Eclipse (Neon) 中创建的 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 库:
- 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
除了 dexmaker .jar 库之外,所有其他库 .jar 都是从从 PowerMock GitHub project 下载的 powermock-mockito-junit-1.6.2.zip 存档中获得的。 dexmaker .jar 库来自dexmaker GitHub project。
为了让这些库能够很好地相互编译,我不得不修改并重新打包 mockito-all-1.10.19.jar 和 powermock-mockito-1.6.2- full.jar 库,因为它们与其他 .jar 库存在以下冲突:
- ./LICENSE、./NOTICE 和 ./asm-license.txt 文件在 mockito-all-1.10.19 中。 jar与cglib-nodep-2.2.2.jar中存在同名文件冲突,所以重命名了
- ./mockito-extensions/org.mockito.plugins.MockMaker 文件 powermock-mockito-1.6.1-full.jar 与存在同名文件冲突在 dexmaker-mockito-1.4.jar 中,因此该文件已从 PowerMock .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)上都尝试了测试用例。
这几乎描述了所有项目设置和来源。我已经尝试在互联网上挖掘了一段时间,但我仍然不知道我的设置中有什么问题或缺失,所以任何帮助将不胜感激。提前致谢!
【问题讨论】:
-
你使用的是 maven、gradle、ivy 还是类似的?
-
没有,我一直在手动管理依赖和构建路径
-
我怀疑如果你开始使用可以解决这个问题的一个。
标签: android eclipse junit mockito powermock