我正在尝试让 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 快照