如何运行单元测试的依赖到Android库模块?

编程入门 行业动态 更新时间:2024-10-23 03:19:35
本文介绍了如何运行单元测试的依赖到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) ...

在上面的示例, DummyData 是 LIB 模块的一部分,而 DummyModel 是应用模块的一部分。 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 ... ...

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

试图十几家不同的事情后,任何帮助深表AP preciated。

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

推荐答案

关键是要添加其他模块的类目录的依赖。所以,你最终 unitTestCompile文件而不是 unitTestCompile项目:

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") ... }

不是很漂亮,也很直观,但它与我目前的设置工作(1.10摇篮,构建工具19.0.1和Android的摇篮 - 插件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库模块?

本文发布于:2023-10-31 04:14:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544972.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   单元测试   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!