【问题标题】:RobolectricContext missing for gradle based project基于 gradle 的项目缺少 RobolectricContext
【发布时间】:2013-05-18 10:29:01
【问题描述】:

使用 robolectric v2.0 和基于 gradle 的项目,我面临着跑步者缺少 RobolectricContext 的问题。它适用于 testCompile 组:'org.robolectric',名称:'robolectric',版本:'2.0-alpha-2'

失败了 testCompile 组:'org.robolectric',名称:'robolectric',版本:'2.0'

我感觉我的问题出在我的 gradle 构建文件中,但我还没有找到解决方法:

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'
}

这是我得到的错误:

ligi@ligi-tp:~/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

【问题讨论】:

  • RobolectricContext 在 Robolectric 2.0 的源代码中不再可用。我目前正在寻找一种迁移的好方法。

标签: android unit-testing testing robolectric


【解决方案1】:

不再需要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);
}

【讨论】:

  • 或者,更简单的是,使用@Config(manifest="MyFile.xml") 注释,或者创建一个名为“org.robolectric.Config.properties”的文件。应该不再需要扩展 RobolectricTestRunner。
  • 感谢您的回答,但我不想使用自定义 Runner - 实际上我使用的是默认的:import org.robolectric.RobolectricTestRunner; @RunWith(RobolectricTestRunner.class) 但无论如何,Runner.java 都会生成失败的代码。
【解决方案2】:

一种解决方法是不要删除 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);
    }

}

【讨论】:

    猜你喜欢
    • 2021-08-01
    • 2015-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多