android - Mockito 1.9.5 + Android 上的 Dexmaker 在 Eclipse 中出现 NoClassDefFoundError

标签 android eclipse unit-testing mocking mockito

我需要在 Eclipse 中测试 Android 上的一个应用程序。我制作了 Android 测试项目并在其中包含了这个类。我需要一些模拟,所以我使用 Mockito,所以我将以下内容添加到我的测试项目库中:mockito-all-1.9.5.jar、dexmaker-1.0.jar、dexmaker-mockito-1.0.jar

package metrocar.view.test;

import android.os.Handler;
import android.test.InstrumentationTestCase;
import static org.mockito.Mockito.*;

public class OBD2Test extends InstrumentationTestCase {

    public void testInStock() {
        Handler mHandler = mock(Handler.class);
    }
}

但是当我尝试在真实设备上运行它时,我收到此错误。我该如何修复它?

java.lang.NoClassDefFoundError: org.mockito.Mockito
at metrocar.view.test.OBD2Test.testInStock(OBD2Test.java:12)
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.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:1661)

最佳答案

当您运行单元测试时,您所依赖的 JAR(mockito)需要导出到运行测试的设备上。您可以在 Eclipse 中的类路径上放置mockito,这样您的测试就可以编译,但这还不够。 Mockito 似乎需要专门位于 Android 测试项目下的 libs 目录中,并且它应该出现在您的 Android 依赖项中。

关于android - Mockito 1.9.5 + Android 上的 Dexmaker 在 Eclipse 中出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16225686/

相关文章:

java - 启动画面和第二个 Activity 返回

c++ - 为 C++/Boost 库(netbeans 或 eclipse)设置环境

asp.net-mvc - 如何使用 Moq 对自定义 ModelBinder 进行单元测试?

java - 拆分 SurfaceView - Android/Java

android - Firebase Android 找 friend 功能

css - 在eclipse动态web元素中,如何将css链接到webcontent文件夹中的jsp文件

Java 不编译我的源代码

python - 跳过没有装饰器语法的单元测试测试

c# - 测试 ViewModel PropertyChanged 事件

java - 以圆形路径移动 ImageView