我正在用 Robolectric 编写 android 单元测试。当我当前运行测试时,我不断收到上述错误。
我从这里查看并尝试了大量建议的解决方案,但没有一个解决了这个错误。
到目前为止,我已经查看了依赖树,我可以看到使用了两个版本的番石榴:
+--- com.google.guava:guava-jdk5:13.0@jar
+--- com.google.guava:guava:27.0.1-android@jar
注意到这一点后,我尝试:
排除第一个依赖
buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.1' classpath 'com.google.gms:google-services:4.0.1' classpath ('org.robolectric:robolectric:4.1') { exclude group: 'com.google.guava', module: 'guava-jdk5' } } }
将番石榴版本升级到最新
api 'com.google.guava:guava:27.0.1-android'
这是错误的样子:
java.lang.NoSuchMethodError: com.google.common.base.CharMatcher.whitespace()Lcom/google/common/base/CharMatcher;
at org.robolectric.res.StringResources.processStringResources(StringResources.java:19)
at org.robolectric.res.StaxValueLoader.onEnd(StaxValueLoader.java:33)
at org.robolectric.res.StaxDocumentLoader.doParse(StaxDocumentLoader.java:66)
at org.robolectric.res.StaxDocumentLoader.loadResourceXmlFile(StaxDocumentLoader.java:30)
at org.robolectric.res.DocumentLoader.loadFile(DocumentLoader.java:48)
at org.robolectric.res.DocumentLoader.load(DocumentLoader.java:27)
at org.robolectric.res.ResourceTableFactory.parseResourceFiles(ResourceTableFactory.java:149)
at org.robolectric.res.ResourceTableFactory.lambda$newFrameworkResourceTable$0(ResourceTableFactory.java:26)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:50)
at org.robolectric.res.ResourceTableFactory.newFrameworkResourceTable(ResourceTableFactory.java:12)
at org.robolectric.internal.SdkEnvironment.getSystemResourceTable(SdkEnvironment.java:35)
at org.robolectric.ApkLoader.getSystemResourceTable(ApkLoader.java:32)
at org.robolectric.android.internal.ParallelUniverse.injectResourceStuffForLegacy(ParallelUniverse.java:252)
at org.robolectric.android.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:126)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:379)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:252)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
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.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
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:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
这是我目前的测试课:
@RunWith(RobolectricTestRunner.class)
public class MyTestClass {
MyTestFragment myTestFragment;
@Test
public void fragmentIsNotNull() throws Exception{
myTestFragment = MyTestFragment.newInstance();
assertNotNull(myTestFragment);
}
}
我将不胜感激。谢谢。