PowerMock + mockitto + JUnit:调用powermockitto .spy()时的Excepti



我在Eclipse (Neon)中创建的Android测试项目中使用PowerMock。当我运行一个基本的测试用例时,我收到一个ExceptionInInitializationError当调用PowerMockito.spy()

下面是报告的堆栈跟踪:

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"?>
<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>
最后是我在project 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项目下载的 PowerMock -mockito-junit-1.6.2.zip归档文件中获得的。dexmaker .jar库是从dexmaker GitHub项目获得的。

为了使库能够很好地相互编译,我必须修改并重新打包mockito-all-1.10.19.jarpowermock-mockito-1.6.2-full.jar库,因为它们与其他.jar库存在以下冲突:

  • 。/许可, mockito-all-1.10.19.jar中的./asm-license.txt文件与cglib- nodeep -2.2.2.jar中存在同名文件冲突,所以它们被重命名为
  • 。/mockito-extensions/org.mockito.plugins。MockMaker文件 PowerMock -mockito-1.6.1-full.jardexmaker-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)上尝试了测试用例。

这几乎描述了所有的项目设置和源。我已经试着在互联网上挖了一段时间了,但我仍然没有任何线索,什么是错误的或缺失在我的设置,所以任何帮助将非常感激。提前感谢!

你得到这个错误是因为你必须传递一个实例给powermockit。间谍的方法。应该是这样的:

PowerMockito.spy(new MyStaticClass());

相关内容

  • 没有找到相关文章

最新更新