本文介绍了如何运行依赖于 Android 库模块的单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试对依赖于库模块中的类的应用模块中的类运行单元测试时,我都会得到:

Whenever I try to run unit-tests for classes in my app module that depend on classes from an library module, I get this:

java.lang.NoClassDefFoundError: de/ivu/junittest/DummyData
    at de.ivu.junittest.app.DummyModel.<init>(DummyModel.java:16)
    at DummyModelTest.testInstantiation(DummyModelTest.java:7)
    ...

在上面的示例中,DummyDatalib 模块的一部分,而 DummyModelapp 的一部分> 模块.DummyModel 有一个 DummyData 类型的成员,但是在测试类 DummyModelTest 中实例化它会在测试时导致上述异常.

In the above sample, DummyData is part of the lib module, while DummyModel is part of the app module. DummyModel has a member of type DummyData, but instantiating this in the test-class DummyModelTest causes the aforementioned exception at test-time.

项目结构如下:

JUnitTestProject
    app [module]
         src
             main
                 java
                     de.ivu.junittest.app
                         DummyModel.java
                         ...
                 ...
             test
                 java
                     de.ivu.junittest.app
                         DummyModelTest.java
                 ...
    lib [module]
         src
             main
                 java
                     de.ivu.junittest
                         DummyData.java
                         ...
                 ...

app 模块的 build.gradle 包含以下内容:

The build.gradle for the app module contains the following:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

sourceSets {
    unitTest {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/res')
    }
}

configurations {
    unitTestCompile.extendsFrom runtime
    unitTestRuntime.extendsFrom unitTestCompile
}

dependencies {
    compile 'com.android.support:appcompat-v7:+'
    compile project (':lib')
    compile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

    unitTestCompile files("$project.buildDir/classes/release")
    unitTestCompile 'junit:junit:4.+'
    unitTestCompile 'org.robolectric:robolectric:2.+'
    unitTestCompile 'com.google.android:android:4.+'
    unitTestCompile project (':lib')
    unitTestCompile fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

    instrumentTestCompile 'junit:junit:4.+'
    instrumentTestCompile 'org.robolectric:robolectric:2.+'
}

task unitTest(type:Test, dependsOn: assemble) {
    testClassesDir = project.sourceSets.unitTest.output.classesDir
    classpath = project.sourceSets.unitTest.runtimeClasspath
}
check.dependsOn unitTest

最后是三个java类的源代码,从DummyData开始:

And finally the source of the three java-classes, starting with DummyData:

package de.ivu.junittest;

import android.util.Log;

public class DummyData {
    private int data;

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
}

DummyModel 类:

package de.ivu.junittest.app;

import android.util.Log;

import de.ivu.junittest.DummyData;

public class DummyModel {
    private DummyData data = new DummyData();

    public void setData(int data) {
        this.data.setData(data);
    }

    public int getData() {
        return this.data.getData();
    }
}

最后,DummyModelTest:

import static org.junit.Assert.assertEquals;
import org.junit.runner.RunWith;
import org.junit.Test;

import org.robolectric.RobolectricTestRunner;

import de.ivu.junittest.app.DummyModel;

@RunWith(RobolectricTestRunner.class)
public class DummyModelTest {
    @Test
    public void testInstantiation() {
        DummyModel model = new DummyModel();
        model.setData(42);
        assertEquals(model.getData(), 42);
    }
}

在尝试了十多种不同的事情之后,任何帮助都深表感谢.

After trying more than a dozen different things, any help is deeply appreciated.

推荐答案

诀窍是添加其他模块的 classes 目录作为依赖项.所以你最终得到 unitTestCompile files 而不是 unitTestCompile project:

The trick is to add the other modules' classes directories as dependency. So you end up with unitTestCompile files instead of unitTestCompile project:

dependencies {
    ...
    unitTestCompile files("../lib/classes/release")
    ...
}

不是很漂亮,也不是很直观,但它适用于我当前的设置(Gradle 1.10、Build Tools 19.0.1 和 Android-Gradle-plugin 0.8).

Not very beautiful, nor very intuitive, but it works with my current setup (Gradle 1.10, Build Tools 19.0.1, and Android-Gradle-plugin 0.8).

这篇关于如何运行依赖于 Android 库模块的单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 10:51