介绍
请注意,在撰写本文时,robolectric 2.4 是最新版本,不支持appcompat v7
库。将在 robolectric 3.0 版本中添加支持(尚无 ETA)。也ActionBar Sherlock
可能导致 robolectric 出现问题。
要在 Android Studio 中使用 Robolectric,您有 2 个选项:
(选项 1)- 使用 Java 模块在 Android Studio 中运行 JUnit 测试
该技术使用 java 模块进行所有测试,并依赖于您的 android 模块和具有一些魔力的自定义测试运行器:
可以在此处找到说明:http: //blog.blundellapps.com/how-to-run-robolectric-junit-tests-in-android-studio/
还要检查该帖子末尾的链接,以从 android studio 运行测试。
(选项 2)- 使用 robolectric-gradle-plugin 在 Android Studio 中运行 JUnit 测试
我在设置 junit 测试以在 Android Studio 中从 gradle 运行时遇到了一些问题。
这是一个非常基本的示例项目,用于从 Android Studio 中基于 gradle 的项目运行 junit 测试:https ://github.com/hanscappelle/android-studio-junit-robolectric这是使用 Android Studio 0.8.14、JUnit 4.10 测试的, robolectric gradle 插件 0.13+ 和 robolectric 2.3
构建脚本(项目/build.gradle)
构建脚本是项目根目录中的 build.gradle 文件。在那里我不得不将robolectric gradle 插件添加到类路径
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'org.robolectric:robolectric-gradle-plugin:0.13.+'
}
}
allprojects {
repositories {
jcenter()
}
}
项目构建脚本 (App/build.gradle)
在应用程序模块的构建脚本中使用robolectric
插件,添加robolectric
配置并添加androidTestCompile
依赖项。
apply plugin: 'com.android.application'
apply plugin: 'robolectric'
android {
// like any other project
}
robolectric {
// configure the set of classes for JUnit tests
include '**/*Test.class'
exclude '**/espresso/**/*.class'
// configure max heap size of the test JVM
maxHeapSize = '2048m'
// configure the test JVM arguments
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'
// configure whether failing tests should fail the build
ignoreFailures true
// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for {$descriptor.name} with result: ${result.resultType}"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'junit:junit:4.10'
}
创建 JUnit 测试类
现在将测试类放在默认位置(或更新 gradle 配置)
app/src/androidTest/java
并以 Test 结尾(或再次更新配置)命名您的测试类,junit.framework.TestCase
使用@Test
.
package be.hcpl.android.mytestedapplication;
import junit.framework.TestCase;
import org.junit.Test;
public class MainActivityTest extends TestCase {
@Test
public void testThatSucceeds(){
// all OK
assert true;
}
@Test
public void testThatFails(){
// all NOK
assert false;
}
}
执行测试
chmod +x
接下来使用命令行中的 gradlew 执行测试(如果需要,使其可执行)
./gradlew clean test
样本输出:
Executing test for {testThatSucceeds} with result: SUCCESS
Executing test for {testThatFails} with result: FAILURE
android.hcpl.be.mytestedapplication.MainActivityTest > testThatFails FAILED
java.lang.AssertionError at MainActivityTest.java:21
2 tests completed, 1 failed
There were failing tests. See the report at: file:///Users/hcpl/Development/git/MyTestedApplication/app/build/test-report/debug/index.html
:app:test
BUILD SUCCESSFUL
故障排除
替代源目录
就像您可以将 java 源文件放在其他地方一样,您可以移动测试源文件。只需更新 gradlesourceSets
配置。
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
androidTest {
setRoot('tests')
}
}
包 org.junit 不存在
您忘记在应用程序构建脚本中添加 junit 测试依赖项
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'junit:junit:4.10'
}
java.lang.RuntimeException:存根!
您正在使用来自 Android Studio 的运行配置而不是命令行(Android Studio 中的终端选项卡)运行此测试。要从 Android Studio 运行它,您必须更新app.iml
文件以在底部列出 jdk 条目。有关详细信息,请参阅deckard-gradle 示例。
完整的错误示例:
!!! JUnit version 3.8 or later expected:
java.lang.RuntimeException: Stub!
at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
at junit.textui.TestRunner.<init>(TestRunner.java:54)
at junit.textui.TestRunner.<init>(TestRunner.java:48)
at junit.textui.TestRunner.<init>(TestRunner.java:41)
at com.intellij.rt.execution.junit.JUnitStarter.junitVersionChecks(JUnitStarter.java:190)
at com.intellij.rt.execution.junit.JUnitStarter.canWorkWithJUnitVersion(JUnitStarter.java:173)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
错误:JAVA_HOME 设置为无效目录
请参阅此 SO question以获取解决方案。将以下导出添加到您的 bash 配置文件:
export JAVA_HOME=`/usr/libexec/java_home -v 1.7`
完整的错误日志:
ERROR: JAVA_HOME is set to an invalid directory: export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
未找到测试类
如果你想从 Android Studio Junit Test runner 运行你的测试,你必须扩展 build.gradle 文件,以便 android studio 可以找到你编译的测试类:
sourceSets {
testLocal {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
android {
// tell Android studio that the instrumentTest source set is located in the unit test source set
sourceSets {
instrumentTest.setRoot('src/test')
}
}
dependencies {
// Dependencies for the `testLocal` task, make sure to list all your global dependencies here as well
testLocalCompile 'junit:junit:4.11'
testLocalCompile 'com.google.android:android:4.1.1.4'
testLocalCompile 'org.robolectric:robolectric:2.3'
// Android Studio doesn't recognize the `testLocal` task, so we define the same dependencies as above for instrumentTest
// which is Android Studio's test task
androidTestCompile 'junit:junit:4.11'
androidTestCompile 'com.google.android:android:4.1.1.4'
androidTestCompile 'org.robolectric:robolectric:2.3'
}
task localTest(type: Test, dependsOn: assemble) {
testClassesDir = sourceSets.testLocal.output.classesDir
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
check.dependsOn localTest
来自: http: //kostyay.name/android-studio-robolectric-gradle-getting-work/
更多资源
我发现的最好的文章是: