Robolectric与摇篮:找不到资源 [英] Robolectric with Gradle: Resources not found
问题描述
我试图运行我Robolectric新摇篮Android编译系统一起测试,但我被困在访问我的主项目的资源。
I'm trying to run my Robolectric tests together with the new Gradle Android build system, but I'm stuck at accessing the resources of my main project.
我分裂构建成两个独立的项目,以避免的Java
和机器人
摇篮插件之间的冲突,所以目录结构看起来大致是这样的:
I split the build into two separate projects to avoid conflicts between the java
and the android
gradle plugins, so the directory structure looks roughly like this:
.
├── build.gradle
├── settings.gradle
├── mainproject
│ ├── build
│ │ ├── classes
│ │ │ └── debug
│ ├── build.gradle
│ └── src
│ └── main
│ ├── AndroidManifest.xml
│ └── ...
└── test
├── build.gradle
└── src
└── test
└── java
└── ...
└── test
├── MainActivityTest.java
├── Runner.java
├── ServerTestCase.java
└── StatusFetcherTest.java
我的 build.gradle
在测试/
目前看起来是这样的:
My build.gradle
in test/
currently looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.stanfy.android:gradle-plugin-java-robolectric:2.0'
}
}
apply plugin: 'java-robolectric'
repositories {...}
javarob {
packageName = 'com.example.mainproject'
}
test {
dependsOn ':mainproject:build'
scanForTestClasses = false
include "**/*Test.class"
// Oh, the humanity!
def srcDir = project(':mainproject').android.sourceSets.main.java.srcDirs.toArray()[0].getAbsolutePath()
workingDir srcDir.substring(0, srcDir.lastIndexOf('/'))
}
project(':mainproject').android.sourceSets.main.java.srcDirs.each {dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.test.compileClasspath += files(buildDir)
sourceSets.test.runtimeClasspath += files(buildDir)
}
dependencies {
testCompile group: 'com.google.android', name: 'android', version: '4.1.1.4'
testCompile group: 'org.robolectric', name: 'robolectric', version: '2.0-alpha-3'
...
}
邪恶的类路径两轮牛车让我来访问我的主要项目的所有类别,除了研究
,它的存在为的.class
在build目录下的文件,但提出了一个 compileTestJava
任务,在此错误:
The evil classpath hackery allows me to access all classes of my main project, except for R
, which exists as .class
file in the build directory, but raises this error during the compileTestJava
task:
/.../MainActivityTest.java:16: error: cannot find symbol
final String appName = activity.getResources().getString(R.string.app_name);
^
symbol: variable string
location: class R
1 error
:test:compileTestJava FAILED
必须有一个更好的办法来执行Robolectric测试新的构建系统,对吧?
There must be a better way to execute Robolectric tests with the new build system, right?
(的应用的完整源)
推荐答案
我过这个同样的问题跑,这就是我想出了。而不是创建为测试一个单独的项目中,我创造了Robolectric测试设置一个信号源,并增加了新的任务,检查将取决于。从你的问题用一些code,这里的(工作)建立文件的相关位:
I was running across this same issue and this is what I came up with. Instead of creating a separate project for the tests, I created a source set for the Robolectric tests and added a new task that "check" would depend on. Using some of the code from your question, here are the relevant bits of the (working) build file:
apply plugin: 'android'
sourceSets {
testLocal {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
dependencies {
compile 'org.roboguice:roboguice:2.0'
compile 'com.google.android:support-v4:r6'
testLocalCompile 'junit:junit:4.8.2'
testLocalCompile 'org.robolectric:robolectric:2.1'
testLocalCompile 'com.google.android:android:4.0.1.2'
testLocalCompile 'com.google.android:support-v4:r6'
testLocalCompile 'org.roboguice:roboguice:2.0'
}
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
我包括我的依赖块指出的是,为了让我得到这件事中去,我不得不重复了我所有的编译
依赖于我的自定义 testLocal
源集。
I've included my dependencies block to point out that in order for me to get this up and going, I had to repeat all of my compile
dependencies in my custom testLocal
source set.
运行摇篮testLocal
构建并运行刚内的src /测试/ java的
,而运行<$测试C $ C>摇篮检查除了那些在默认的Android instrumentTest源集运行这些测试。
Running gradle testLocal
builds and runs just the tests inside of src/test/java
, while running gradle check
runs these tests in addition to those in the default android instrumentTest source set.
希望这有助于!
这篇关于Robolectric与摇篮:找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!