如何在Android测试中使用Mockito和dexmaker?

时间:2012-09-27 16:00:04

标签: android mockito dexmaker

我想让Mockito(1.9.5 rc1)在我的Android测试中工作(使用Robotium)。为了让它在DVM上运行,我将dexmaker.jar和dexmaker-mockito.jar(v 0.9)放在测试项目的类路径中。

在Eclipse中作为Android JUnit测试进行测试后运行:

package com.stampay.pos.test;

import roboguice.RoboGuice;
import android.app.Application;

import com.jayway.android.robotium.solo.Solo;
import com.stampay.pos.activities.HomeActivity;
import com.stampay.pos.model.Consumer;
import com.stampay.pos.util.ScannerAndroid;

import static org.mockito.Mockito.*;

public class HomeActivityTest extends ActivityTest<HomeActivity> {
    private Solo solo;
    Application app;

    public HomeActivityTest() {
        super(HomeActivity.class);

    }

    @Override
    public void setUp() throws Exception {
        solo = new Solo(getInstrumentation(), getActivity());
    }

    @Override
    public void tearDown() throws Exception {
        solo.finishOpenedActivities();
        RoboGuice.util.reset();
    }

    public void testScan() throws Exception {

        ScannerAndroid scanner = (ScannerAndroid) getActivity().getScannerHelper();

        Consumer mockConsumer = mock(Consumer.class);

        //scanner.generateResult("consumer1");
    }
}

我得到以下堆栈跟踪:

java.lang.ExceptionInInitializerError
at com.stampay.pos.test.HomeActivityTest.testScan(HomeActivityTest.java:41)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:192)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1584)
Caused by: java.lang.ExceptionInInitializerError
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.Mockito.<clinit>(Mockito.java:932)
... 15 more
Caused by: java.lang.ExceptionInInitializerError
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
... 17 more
Caused by: org.mockito.exceptions.misusing.MockitoConfigurationException: Failed to load interface org.mockito.plugins.MockMaker using jar:file:/data/app/com.stampay.pos.test-2.apk!/mockito-extensions/org.mockito.plugins.MockMaker
... 15 more
Caused by: java.lang.ClassNotFoundException: com.google.dexmaker.mockito.DexmakerMockMaker
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:133)
at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:102)
at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:61)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.Mockito.<clinit>(Mockito.java:932)
... 15 more

有什么建议吗? http://code.google.com/p/dexmaker/http://code.google.com/p/mockito/wiki/ReleaseNotes不提供任何有用的信息。

这里似乎报告了一个类似的问题:http://www.paulbutcher.com/2012/05/mockito-on-android-step-by-step/#comment-65955

更新: 来自https://github.com/paulbutcher/mockito-on-android/tree/master/WarehouseManagerTest/libs的.jar文件一切顺利,尽管他使用了基于1.9.1的Mockito过时快照

1 个答案:

答案 0 :(得分:3)

我在尝试使用mockito 1.9.5-rc1时遇到了同样的问题。

dexmaker页面notes that you need 1.9.5+为此,显然RC1没有削减它。 Mockito 1.9.5 appears to have just been released days ago,所以它应该很快会出现在Maven Central。

与此同时,你可以download the 1.9.5 zip from Mockito's site。如果您使用的是Maven,则可以使用mvn install:install-file手动将这些jar安装到您的本地仓库。我还通过检查最新的Mockito源(未发布的1.9.8),构建并安装到我当地的Maven仓库来实现这一目的。