如何使用 Robolectric 代码进行测试,即使用比 Robolectric 需要的旧番石榴版本

问题描述

我在使用 Robolectric 4.51 版测试使用番石榴版本 19(旧番石榴版本)的代码和库的 android 代码时遇到问题 Robolectric 测试套件需要比 19 更新的 guava 版本,否则会输出

java.lang.NoSuchMethodError: com.google.common.collect.ImmutableList.sortedcopyOf(Ljava/util/Comparator;Ljava/lang/Iterable;) Lcom/google/common/collect/ImmutableList;

at org.robolectric.internal.bytecode.ShadowProviders.<init>(ShadowProviders.java:25)
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.robolectric.util.inject.Injector.inject(Injector.java:250)
at org.robolectric.util.inject.Injector.lambda$memoized$1(Injector.java:232)
at org.robolectric.util.inject.Injector$MemoizingProvider.get(Injector.java:498)
at org.robolectric.util.inject.Injector.getInstanceInternal(Injector.java:224)
at org.robolectric.util.inject.Injector.getInstance(Injector.java:208)
at org.robolectric.util.inject.Injector.getInstance(Injector.java:202)
at org.robolectric.internal.SandBoxTestRunner.<init>(SandBoxTestRunner.java:78)
at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:103)
at org.robolectric.RobolectricTestRunner.<init>(RobolectricTestRunner.java:98)
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.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

如果我定义以下语句,我可以很好地编译 Robolectric 测试, 但随后我的应用程序崩溃,因为番石榴版本 19 也将在应用程序代码中替换为 版本 27.0.1。

testImplementation 'com.google.guava:guava'
constraints {
    implementation('com.google.guava:guava:27.0.1-jre') {
        because 'Robolectric has to use this version for tests'
    }
}

我进一步尝试使用 testIplementation ('com.google.guava:guava:27.0.1-jre') 但是随后编译 Robolectric 测试仍然失败并出现相同的错误

我进一步尝试执行以下操作:

testRuntimeOnly 'com.google.guava:guava:27.0.1-jre'
testImplementation 'com.google.guava:guava:27.0.1-jre'
testImplementation ("org.robolectric:robolectric:$ROBOLECTRIC_VERSION"){
    exclude group: 'com.google.guava',module: 'guava'
}

这个试验也返回同样的错误

我已经通过了以下提案: https://github.com/robolectric/robolectric/issues/5147

但他们只是强制更新番石榴,这在我的情况下不是一个选项,因为被测代码使用的是旧版本的番石榴,然后会失败......而且我无法更改此代码,因为它来自另一个项目团队,并且共享代码...

我使用的是 Gradle 6.5,我的问题如下: 我如何才能实现只有 Robolectric Testsuite 与 guava 27 一起运行, 应用程序和测试代码仍然使用旧的番石榴版本 19?

预先感谢您的任何建议!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)