0

我添加了Robolectric以获得对 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)






我感谢您的帮助!

4

1 回答 1

0

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

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

于 2021-12-14T09:30:33.503 回答