robolelectric + JaCoCo |在运行单元测试且未生成代码覆盖率时出现问题



我已经添加了robolelectric以获得Shadow api对编写单元测试的支持。能够通过Android Studio中默认的Intellij代码覆盖率工具获得覆盖率百分比。

现在尝试添加JaCoCo以获得增强的覆盖报告。

我使用文件build.gradle(app)
插件作为apply from: "$rootDir/jacoco.gradle"
依赖项作为testImplementation 'org.robolectric:robolectric:4.3.1'


jacoco.gradle

apply plugin: 'jacoco'
/*jacoco {
// https://bintray.com/bintray/jcenter/org.jacoco:org.jacoco.core
toolVersion = "0.7.1.201405082137"
}*/
android {
testOptions {
unitTests {
includeAndroidResources = true
}
unitTests.all {
systemProperty 'user.home', System.getenv('HOME')
jacoco {
includeNoLocationClasses = true
}
}
unitTests {
returnDefaultValues = true
}
}
}
project.afterEvaluate {
android.applicationVariants.all { variant ->
def name = variant.name
def testTaskName = "test${name.capitalize()}UnitTest"
tasks.create(name: "${testTaskName}Coverage", type: JacocoReport, dependsOn: "$testTaskName") {
group = "Reporting"
description = "Generate Jacoco coverage reports for the ${name.capitalize()} build."
classDirectories.from = fileTree(
dir: "${project.buildDir}/intermediates/classes/${name}",
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
sourceDirectories.from = files(['src/main/java'].plus(android.sourceSets[name].java.srcDirs))
executionData.from = files("${project.buildDir}/jacoco/${testTaskName}.exec")
reports {
xml.enabled = true
html.enabled = true
}
}
}
}

通过CL/Run(Button)或Jenkins运行测试用例时,得到相同的错误

com.example.LoginActivityTest > initializationError FAILED
java.lang.RuntimeException at AndroidJUnit4.java:121
Caused by: java.lang.reflect.InvocationTargetException at null:-1
Caused by: java.lang.NoSuchMethodError at ShadowProviders.java:25
com.example.HomeActivityTest > initializationError FAILED
java.lang.RuntimeException at AndroidJUnit4.java:121
Caused by: java.lang.reflect.InvocationTargetException at null:-1
Caused by: java.lang.NoSuchMethodError at ShadowProviders.java:25

=*=*=*=*=*=*=*=*=*=*
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)



我很感激你的帮助!

发生这种情况是因为在我的类路径中有多个版本的Guava。其中一个是由Gradle包含的,而另一个可能是其他jar文件的一部分。您需要遍历jar文件依赖项并找出其他guava来自哪里,然后删除它。

更多信息请参考https://github.com/robolectric/robolectric/issues/5147

相关内容

  • 没有找到相关文章

最新更新