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需要一个对象Context,因此我创建了一个模拟对象(带有模拟对象).我也尝试了一个对象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
- Android 2.1 -> android.jar
- Android依赖项 -> Annotations.jar
- Junit 3 -> junit.jar
- mockito-all-1.9.5.jar
- Android 2.1 -> android.jar
- Android Dependencies -> annotations.jar
- Junit 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屋!