使用 Mockito 进行 Android 仪器测试



我正在尝试将 Mockito 框架与 android 仪器测试一起使用,但我无法正确初始化它。我有以下测试类:

class MainKontorTest extends ActivityInstrumentationTestCase2<MainActivity> {
    @Mock Dependency bar;
    @Override
    public void setUp() {
        super.setUp();
        MockitoAnnotations.initMocks(this);
    }
    public void testSomething() {
        Foo foo = new Foo(bar);
    }
}

当我尝试运行此测试时,出现以下错误和堆栈跟踪:

java.lang.ExceptionInInitializerError at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95) at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57) at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49) at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24) at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33) at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59) at org.mockito.Mockito.mock(Mockito.java:1285) at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:33) at org.mockito.internal.configuration.MockAnnotationProcessor.process(MockAnnotationProcessor.java:16) at org.mockito.internal.configuration.DefaultAnnotationEngine.createMockFor(DefaultAnnotationEngine.java:43) at org.mockito.internal.configuration.DefaultAnnotationEngine.process(DefaultAnnotationEngine.java:66) at org.mockito.internal.configuration.InjectingAnnotationEngine.processIndependentAnnotations(InjectingAnnotationEngine.java:71) at org.mockito.internal.configuration.InjectingAnnotationEngine.process(InjectingAnnotationEngine.java:55) at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:108) at org.arkadiy.moduledelegationsample.ui.main.MainKontorTest.setUp(MainKontorTest.java:20) at junit.framework.TestCase.runBare(TestCase.java:132) at junit.framework.TestResult$1.protect(TestResult.java:115) at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77) at junit.framework.TestResult.run(TestResult.java:118) at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55) at junit.framework.TestCase.run(TestCase.java:124) at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63) at junit.framework.TestSuite.runTest(TestSuite.java:243) at junit.framework.TestSuite.run(TestSuite.java:238) at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103) at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69) at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1869) 由:org.mockito.cglib.core.CodeGenerationException:java.lang.reflect.InvocationTargetException-->null at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:238) at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109) at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105) at org.mockito.cglib.proxy.Enhancer.(增强.java:70) ...40 更多 由: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at org.mockito.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:385) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:220) ...45 更多 原因:java.lang.UnsupportedOperationException:无法加载这种类型的类文件 at java.lang.ClassLoader.defineClass(ClassLoader.java:300) ...49 更多

如何将 Mockito 与仪器测试一起使用?我尝试将Mockito与较新的规则api一起使用,但错误是相同的。

Mockito

创建的模拟是生成的类文件;但是,Mockito是为在JVM中使用而设计的,因此开箱即用,它使用cglib生成.class文件。Android 仪器测试用例在设备或模拟器上运行,因此它们需要 Dalvik .dex文件。

您需要确保在类路径中包含 DexMaker。dexmaker-mockito Maven项目似乎是对的,尽管它依赖于Mockito 1.10.5,它在这一点上落后了几个版本。

作为旁注,除非您使用 Android 测试支持库,否则您将需要使用 JUnit3 语义。您将无法使用@Rule字段或自定义测试运行程序;您还需要覆盖setUptearDown(而不是@Before@After注释)并将测试命名为testFooBar(而不是@Test注释)。

相关内容

  • 没有找到相关文章

最新更新