robolectric java.lang.NoSuchFieldException:sSystem

问题描述 投票:4回答:2

在使用robolectric进行UnitTest时,我收到错误

java.lang.NoSuchFieldException:sSystem

请给我一个建议。

描述

集成的robolectric错误

java.lang.RuntimeException: java.lang.RuntimeException: java.lang.NoSuchFieldException: sSystem

at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:332)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:259)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:41)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:199)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: sSystem
at org.robolectric.util.ReflectionHelpers.setStaticField(ReflectionHelpers.java:174)
at org.robolectric.shadows.ShadowAssetManager.reset(ShadowAssetManager.java:780)
at org.robolectric.Shadows.reset(Shadows.java:1644)
at org.robolectric.Robolectric.reset(Robolectric.java:35)
at org.robolectric.internal.ParallelUniverse.resetStaticState(ParallelUniverse.java:52)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:319)
... 19 more
Caused by: java.lang.NoSuchFieldException: sSystem
at java.lang.Class.getDeclaredField(Class.java:2070)
at org.robolectric.util.ReflectionHelpers.setStaticField(ReflectionHelpers.java:172)
... 24 more

重现步骤

Robolectric和Android版本

compileSdkVersion 25
buildToolsVersion "25.0.2"
minSdkVersion 15
targetSdkVersion 25
testCompile 'org.robolectric:robolectric:3.2.2'
robolectric
2个回答
3
投票

由于以前存储库下载失败,我遇到了同样的问题。当我针对sdk 23时,我删除了6.0.1_r3-robolectric-r1目录,触发了重新下载并解决了这个问题。

rm -rf /home/user/.m2/repository/org/robolectric/android-all/6.0.1_r3-robolectric-r1


1
投票

我在kotlin测试中有这个并通过打开我的测试类来修复它:

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [23], constants = BuildConfig::class)
open class MyExampleTest {

应用于java这意味着您的测试类不应该是最终的

.

© www.soinside.com 2019 - 2024. All rights reserved.