2013-05-18 90 views
3

與Robolectric的v2.0和基於gradle的項目我正面臨賽跑者缺少RobolectricContext的問題。它的工作原理與 testCompile組: 'org.robolectric',名稱: 'robolectric',版本: '2.0-α-2'RobolectricContext缺少基於Gradle的項目

和失敗 testCompile組: 'org.robolectric',名稱: 'robolectric'版本:「2.0」

我有我的問題是在我的gradle這個build文件的感覺,但我發現沒有辦法解決它尚未:

apply plugin : 'java-robolectric' 
apply plugin : 'idea' 

// get 'java-robolectric' from Maven Central 
buildscript { 
    repositories { 
    mavenCentral() 
    } 
    dependencies { 
    // use version 2.0 for Robolectric 2.0 
    classpath group: 'com.stanfy.android', name: 'gradle-plugin-java-robolectric', version: '2.0' 
    } 
} 
sourceSets { 
    main { 
     java { 
      srcDir 'src/java' 
     } 
    } 
} 

version = '0.9' 

javarob { 
    packageName = 'org.ligi.androidhelper' 
} 

test { 
    scanForTestClasses = false 
    include "**/*Test.class" 
} 

repositories { 
    mavenCentral() 
} 

test { 
    afterTest { desc, result -> 
     println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}" 
    } 
} 

dependencies { 
    compile fileTree(dir : 'libs', include : '*.jar') 

    testCompile group: 'junit', name: 'junit', version: '4.10' 
    testCompile group: 'org.mockito', name: 'mockito-core', version: '1.8.0' 

    compile group: 'com.google.android', name: 'android', version: '4.1.1.4' 
    testCompile group: 'org.robolectric', name: 'robolectric', version: '2.0' 
} 

是錯誤,我得到:

[email protected]:~/git/AndroidHelper$ gradle test 
:compileJava UP-TO-DATE 
:processResources UP-TO-DATE 
:classes UP-TO-DATE 
:compileTestJava UP-TO-DATE 
:processTestResources UP-TO-DATE 
:testClasses UP-TO-DATE 
:test 
Executing test classMethod [org.ligi.androidhelper.test.CheckBoxHelperTest] with result: FAILURE 

org.ligi.androidhelper.test.CheckBoxHelperTest > classMethod FAILED 
    java.lang.RuntimeException 
     Caused by: java.lang.RuntimeException 
Executing test classMethod [org.ligi.androidhelper.test.BitmapHelperTest] with result: FAILURE 

org.ligi.androidhelper.test.BitmapHelperTest > classMethod FAILED 
    java.lang.RuntimeException 
     Caused by: java.lang.RuntimeException 

2 tests completed, 2 failed 
:test FAILED 

FAILURE: Build failed with an exception. 

* What went wrong: 
Execution failed for task ':test'. 
> There were failing tests. See the report at: file:///home/ligi/git/AndroidHelper/build/reports/tests/index.html 

* Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 

BUILD FAILED 

Total time: 11.723 secs 

完整的數據來源在這裏: https://github.com/ligi/AndroidHelper

+0

RobolectricContext不再可用於Robolectric 2.0的源代碼中。我目前正在尋找一種遷移的好方法。 – keyboardsurfer

回答

0

一種解決方法是不去除Runner.java - 因爲它總是得到由robolectric插件的gradle與故障代碼(使用的自動生成RobolectricContex)然後。訣竅是修改此文件 - 即使您實際上不打算使用它 - 我的看起來像這樣:

import java.io.File; 
import org.junit.runners.model.InitializationError; 
import org.robolectric.AndroidManifest; 
import org.robolectric.RobolectricTestRunner; 

/**              
* Use this runner instead of RobolectricTestRunner with @RunWith annotation. 
*/ 
public class Runner extends RobolectricTestRunner { 

    public Runner(final Class<?> testClass) throws InitializationError { 
     super(testClass); 
    } 

} 
2

該類別RobolectricContext不再需要。此外,它在Robolectric 2.0中不存在。您可以簡單地覆蓋RobolectricTestRunner中的方法。

例如,找到AndroidManifest.xml可以通過以下方式實現:

@Override 
protected AndroidManifest createAppManifest(FsFile manifestFile) { 
    if (!manifestFile.exists()) { 
    manifestFile = Fs.fileFromPath("pathToMy/AndroidManifest.xml"); 
    } 
    return super.createAppManifest(manifestFile); 
} 
+1

或者,更容易使用@Config(manifest =「MyFile.xml」)註釋,或者創建一個名爲「org.robolectric.Config.properties」的文件。應該幾乎不需要再擴展RobolectricTestRunner。 – Xian

+0

感謝您的答案,但我不想使用自定義亞軍 - 實際上我使用默認的一個: import org.robolectric.RobolectricTestRunner; @RunWith(RobolectricTestRunner.class) 但無論如何,Runner.java都會生成失敗的代碼。 – ligi