当我使用Robolectric + PowerMock + Jacoco做Android LLT时,Jacoco无法使用PowerMock类

时间:2017-08-10 03:54:04

标签: android powermock robolectric jacoco

我的build.gradle是这样的:

ext {
    robolectricVersion = "3.3.2"
    powerMockVersion = "1.6.6"
}

dependencies {
        classpath 'com.android.tools.build:gradle:2.3.2'
        classpath 'org.ajoberstar:gradle-jacoco:0.2.0'
    }
dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile fileTree(dir: 'lib', include: '*.jar')



    testCompile 'junit:junit:4.12'
    testCompile 'org.assertj:assertj-core:1.7.0'
    testCompile "org.robolectric:robolectric:${robolectricVersion}"
    testCompile "org.mockito:mockito-core:${powerMockVersion}"
    testCompile "org.powermock:powermock-module-junit4:${powerMockVersion}"
    testCompile "org.powermock:powermock-module-junit4-rule:${powerMockVersion}"
    testCompile "org.powermock:powermock-api-mockito2:${powerMockVersion}"
    testCompile "org.powermock:powermock-classloading-xstream:${powerMockVersion}"

}

jacoco {
    toolVersion "0.7.1.201405082137"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

def coverageSourceDirs = [
        'src/'
]

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebugUnitTest") {
    group = "Reporting"

    description = "Generate Jacoco coverage reports"

    classDirectories = fileTree(
            dir: 'build/intermediates/classes/debug',
            excludes: ['**/R.class',
                       '**/*$Proxy.class',
                       '**/*$Stub.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']          // 去掉不进行检查的文件
    )

    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/jacoco/testDebugUnitTest.exec')

    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }

    reports {
        xml.enabled true
        csv.enabled true
        html.enabled true
        html.destination "${buildDir}/jacocoHtml"
        xml.destination "${buildDir}/jacocoXml.xml"
        csv.destination "${buildDir}/jacocoCsv.csv"
    }
}

jacoco {
    toolVersion "0.7.1.201405082137"
    reportsDir = file("$buildDir/customJacocoReportDir")
}

def coverageSourceDirs = [
        'src/'
]

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebugUnitTest") {
    group = "Reporting"

    description = "Generate Jacoco coverage reports"

    classDirectories = fileTree(
            dir: 'build/intermediates/classes/debug',
            excludes: ['**/R.class',
                       '**/*$Proxy.class',
                       '**/*$Stub.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']          // 去掉不进行检查的文件
    )

    additionalSourceDirs = files(coverageSourceDirs)
    sourceDirectories = files(coverageSourceDirs)
    executionData = files('build/jacoco/testDebugUnitTest.exec')

    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }

    reports {
        xml.enabled true
        csv.enabled true
        html.enabled true
        html.destination "${buildDir}/jacocoHtml"
        xml.destination "${buildDir}/jacocoXml.xml"
        csv.destination "${buildDir}/jacocoCsv.csv"
    }
}

然后,当我跑:     gradlew clean testDebugUnitTest jacocoTestReport --info 我得到这样的结果:

[ant:jacocoReport]包中的课程' DIAL'与执行数据不匹配。对于  报告生成必须在运行时使用相同的类文件。 [ant:jacocoReport] com / huawei / restserver / stb / utils / Nati类的执行数据 veUtil不匹配。

当我检查报告时,发现NativeUtil.class覆盖率为0%

请帮帮我,谢谢。

0 个答案:

没有答案