每当我尝试为我的app模块中依赖于库模块中的类的类运行单元测试时,我得到这个:
java.lang.NoClassDefFoundError: de/ivu/junittest/DummyData
at de.ivu.junittest.app.DummyModel.<init>(DummyModel.java:16)
at DummyModelTest.testInstantiation(DummyModelTest.java:7)
...
在上面的示例中,DummyData
是lib
模块的一部分,而DummyModel
是app
模块的一部分。 DummyModel
的成员类型为DummyData
,但在测试类DummyModelTest
中实例化会导致上述异常在测试时发生。
项目结构如下:
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
...
...
build.gradle
模块的app
包含以下内容:
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
开始:
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);
}
}
在尝试了十几种不同的东西后,任何帮助都深受赞赏。
答案 0 :(得分:1)
诀窍是将其他模块的classes
目录添加为依赖项。因此,您最终得到unitTestCompile files
而不是unitTestCompile project
:
dependencies {
...
unitTestCompile files("../lib/classes/release")
...
}
不是很漂亮,也不是非常直观,但它适用于我当前的设置(Gradle 1.10,Build Tools 19.0.1和Android-Gradle-plugin 0.8)。