java.lang.NoClassDefFoundError:android 和 junit 测试 [英] java.lang.NoClassDefFoundError:android and junit test
问题描述
我发现我不是唯一遇到此问题的人,但我没有找到正确的答案.我有一个想要测试的 android 项目.我为我的项目的每个类创建了一个 junit 测试类.我的问题是当我运行测试时,出现以下错误:
I saw that I'm not the only one having this problem but I don't find a correct answer. I have an android project that I want to test. I create a junit test class for each class of my project. My problem is when I run my test, I have the following error :
java.lang.NoClassDefFoundError: android/content/Context
这是我的课堂测试:
public class DevicesBDDTest extends TestCase {
DevicesBDD bdd;
/**
* @throws java.lang.Exception
*/
protected static void setUpBeforeClass() throws Exception {
}
/**
* @throws java.lang.Exception
*/
protected static void tearDownAfterClass() throws Exception {
}
protected void setUp() throws Exception {
super.setUp();
Context ctx = mock(Context.class);
final MaBaseSQLiteInterface mockMaBaseSQLite = mock(MaBaseSQLiteInterface.class);
bdd = new DevicesBDD(ctx){
@Override
public MaBaseSQLiteInterface createMaBaseSQlite(Context context) {
return mockMaBaseSQLite;
}
};
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void test() {
assertEquals(1, 1);
}
}
我的类 DevicesBDD 需要一个对象上下文,因此我创建了一个模拟(使用 mockito).我也尝试过使用 MockContext 对象,但它不起作用.
My class DevicesBDD has needs an object Context, therefore I create a mock (with mockito). I tried with a object MockContext too, but it's doesn't work.
这是我的 Java 构建路径:
This is my Java Build Path :
- mockito-all-1.9.5.jar
- 安卓 2.1-> android.jar
- Android 依赖-> annotations.jar
- 六月 3-> junit.jar
推荐答案
不确定我是否和你有同样的问题,但我正在使用 gradle 并且由于某种原因测试不再运行,与相同的错误你有过.我尝试清理和重建,但无济于事.经过数小时的沮丧并试图找到答案后,我在 GitHub 线程中找到了一个简单的解决方案:
Not sure if I had the same problem as you but I am using gradle and for some reason the tests just wouldn't run anymore, with the same error as you had. I tried cleaning and rebuilding but to no avail. After hours of frustration and trying to find an answer I came across the simple solution in a GitHub thread:
我通过删除项目中的 .gradle 文件夹并重建项目解决了这个问题.
I resolved this issue by removing the .gradle folder in my project and rebuilding the project.
(感谢 vpetrov)
(thanks to vpetrov)
这篇关于java.lang.NoClassDefFoundError:android 和 junit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!