简体   繁体   English

java.lang.NoClassDefFoundError:android 和 junit 测试

[英]java.lang.NoClassDefFoundError:android and junit test

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.我有一个想要测试的 android 项目。 I create a junit test class for each class of my project.我为我的项目的每个类创建了一个 junit 测试类。 My problem is when I run my test, I have the following error :我的问题是当我运行测试时,出现以下错误:

java.lang.NoClassDefFoundError: android/content/Context

This is my class test :这是我的课堂测试:

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);
    }
}

My class DevicesBDD has needs an object Context, therefore I create a mock (with mockito).我的类 DevicesBDD 需要一个对象上下文,因此我创建了一个模拟(使用 mockito)。 I tried with a object MockContext too, but it's doesn't work.我也尝试过使用 MockContext 对象,但它不起作用。

This is my Java Build Path :这是我的 Java 构建路径:

  • mockito-all-1.9.5.jar mockito-all-1.9.5.jar
  • Android 2.1 -> android.jar Android 2.1 -> android.jar
  • Android Dependencies -> annotations.jar Android 依赖项 -> annotations.jar
  • Junit 3 -> junit.jar Junit 3 -> junit.jar

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.不确定我是否和你有同样的问题,但我正在使用 gradle 并且由于某种原因,测试不再运行,并且出现与你相同的错误。 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:经过数小时的沮丧并试图找到答案后,我在 GitHub 线程中发现了一个简单的解决方案:

I resolved this issue by removing the .gradle folder in my project and rebuilding the project.我通过删除项目中的 .gradle 文件夹并重建项目解决了这个问题。

(thanks to vpetrov) (感谢 vpetrov)

您可以在终端中运行./gradlew clean test

Fixed the issue by following these steps --按照以下步骤修复了问题——

1.Open module level build.gradle file, go to dependencies, go to this line -- 1.打开模块级别的build.gradle文件,进入依赖项,进入这一行——

testImplementation 'junit:junit:4.12'

2.Change the junit version to anything else below it (like 4.10) 2.将junit版本更改为低于它的任何其他版本(如4.10)

testImplementation 'junit:junit:4.10'

3.Sync project 3.同步项目

4.The issue fixed at this point in my case 4.在我的情况下此时已解决的问题

5.Set the junit version back to what it was before (4.12 in my case) if you want 5.如果需要,将junit版本设置回之前的版本(在我的情况下为4.12)

testImplementation 'junit:junit:4.12'

6.Sync project 6.同步项目

Changing the junit version and syncing project worked in my case.更改 junit 版本和同步项目在我的情况下有效。

Robolectric 4.4 版似乎不支持 jdk 14。所以我可以切换到 jdk 13 或将 robolectric 更新到4.5-alpha-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM