[英]Robolectric with Gradle: Resources not found
我正在嘗試與新的Gradle Android構建系統一起運行我的Robolectric測試,但我一直在訪問我的主項目的資源。
我將構建拆分為兩個獨立的項目,以避免java
和android
gradle插件之間的沖突,因此目錄結構看起來大致如下:
.
├── 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
in test/
目前看起來像這樣:
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'
...
}
邪惡的classpath hackery允許我訪問我的主項目的所有類,除了R
,它在build目錄中以.class
文件的形式存在,但在compileTestJava
任務期間引發了這個錯誤:
/.../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測試,對嗎?
( 應用程序的完整來源 )
我遇到了同樣的問題,這就是我提出的問題。 我沒有為測試創建單獨的項目,而是為Robolectric測試創建了一個源集,並添加了一個“check”依賴的新任務。 使用您問題中的一些代碼,這里是(工作)構建文件的相關位:
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
源集中重復所有compile
依賴項。
運行gradle testLocal
構建並運行src/test/java
,而運行gradle check
除了運行默認的android instrumentTest源集之外,還運行這些測試。
希望這可以幫助!
更新:傑克沃頓剛剛宣布了gradle-android-test-plugin
。 您可以在https://github.com/square/gradle-android-test-plugin找到它
它看起來非常精簡,特別是如果你打算使用robolectric
。
下面的舊答案
robolectric-plugin
看起來很有希望。
他們提供的示例build.gradle
文件是:
buildscript {
repositories {
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
classpath 'com.novoda.gradle:robolectric-plugin:0.0.1-SNAPSHOT'
}
}
apply plugin: 'android'
apply plugin: 'robolectric'
repositories {
mavenCentral()
mavenLocal()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
dependencies {
//compile files('libs/android-support-v4.jar')
// had to deploy to sonatype to get AAR to work
compile 'com.novoda:actionbarsherlock:4.3.2-SNAPSHOT'
robolectricCompile 'org.robolectric:robolectric:2.0'
robolectricCompile group: 'junit', name: 'junit', version: '4.+'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 17
}
}
它似乎不適用於Android Gradle插件版本0.5,但也許很快就會出現。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.