android - Robospock 初始化错误 NoSuchMethodError 与 Android Studio 3.0

标签 android android-studio gradle robolectric robospock

我无法获得在 Android Studio 3.0 beta 6 和 gradle 3.4.1 中运行的新 Robospock 规范。我在 Android Studio 中运行测试时得到的错误是:

java.lang.NoSuchMethodError: org.robolectric.annotation.Config$Implementation.<init>(Lorg/robolectric/annotation/Config;)V

    at org.robospock.internal.RoboSputnik.<clinit>(RoboSputnik.java:52)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:49)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

规范看起来像这样:

import org.robolectric.annotation.Config
import org.robospock.RoboSpecification
@Config(constants = com.myapp.BuildConfig, sdk = 19, manifest='src/main/AndroidManifest.xml')
public class MyActivitySpec extends RoboSpecification {
  ...
}

我的 gradle 依赖项是:

testImplementation 'org.robolectric:robolectric:3.4.2'
testImplementation('org.robospock:robospock:1.0.1') {
    exclude module: 'robolectric'
}

以上转换为所有 robolectric 传递依赖项(相当大)和以下 robospock 依赖项:

\--- org.robospock:robospock:1.0.1
     \--- org.spockframework:spock-core:1.0-groovy-2.4
          +--- org.codehaus.groovy:groovy-all:2.4.1
          \--- junit:junit:4.12 (*)

我也试过如下创建自定义 Runner:

public class MyTestRunner extends RobolectricTestRunner {

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

    @Override protected Config buildGlobalConfig() {
        return new Config.Builder().setApplication(com.me.MyApp).setConstants(com.myapp.BuildConfig).setSdk(19).setManifest('src/main/AndroidManifest.xml').build();
    }
}

并使用类注释在我的规范中指定它:@RunWith(MyTestRunner)

在这种情况下,我得到以下错误:

java.lang.Exception: No runnable methods

    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:191)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:128)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:416)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
    at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
    at org.robolectric.internal.SandboxTestRunner.<init>(SandboxTestRunner.java:43)
    at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:77)

以防万一,我在 Android Studio 中的运行配置是 Android Junit 配置。

任何指导的 TIA。

最佳答案

Robospock 差不多一年没有更新了,所以我最好的猜测是它与 Robolectric 不兼容 您提供的版本。

testImplementation 'org.robolectric:robolectric:3.4.2'
testImplementation('org.robospock:robospock:1.0.1') {
    exclude module: 'robolectric'
}

您手动选择了一个不同的版本,即库支持的版本,即 3.0

错误是关于缺少 Config$Implementation 构造函数,它在 master 上丢失: https://github.com/robolectric/robolectric/blob/master/annotations/src/main/java/org/robolectric/annotation/Config.java#L175

修复它需要更新库。

关于android - Robospock 初始化错误 NoSuchMethodError 与 Android Studio 3.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46314008/

相关文章:

Android studio 2.1.2 gradle aapt语法错误: Unterminated quoted string

git - git describe --match在TeamCity上不起作用

gradle 获取子项目名称、版本

gradle - Gradle JacocoReport任务触发令人困惑的异常

android - SurfaceView 相机预览代码在 Pre Lollipop 上运行良好但在 Lollipop 设备上崩溃

java - 当我的 ListView 滚动到底部时如何加载新元素?

android - 如何在两个 fragment onClick 按钮之间传递参数

android - 按下主页按钮后播放器变得空 Android

android - 如何在底部导航 View 中添加分隔线

android - 我应该如何降低最低SDK版本?