【问题标题】:Gradle Jacoco does not track the coverage of Spock test when using PowermockGradle Jacoco 在使用 Powermock 时不跟踪 Spock 测试的覆盖率
【发布时间】:2015-05-19 12:32:17
【问题描述】:

我正在使用 Gradle Jacoco 插件来记录我们软件的测试覆盖率。该软件包含一些大量使用静态方法的遗留代码。为了模拟它们,我们提出了 Powermock 并将其集成到我们的 Spock 测试中。

一切正常。 唯一Jacoco 不跟踪使用 Powermock 的测试的测试覆盖率。普通 Spock 测试(不使用 Powermock 的测试)的测试覆盖率按预期报告。

有人知道如何让Jacoco 记录我的 Powermock 测试的覆盖率吗?

请在下面找到 Spock 测试。

@PrepareForTest([CodeCacheManager])
class SampleSpec extends Specification {

 @Rule
 PowerMockRule powerMockRule = new PowerMockRule();

 @Unroll
 void "Convert CodeIdentifier #insertvalue toString #returnvalue"() {

    given:
    def converter = new CodeIdentifierCodeInternalNameCustomConverter()

    and:
    mockStatic(CodeCacheManager.class)
    Mockito.when(CodeCacheManager.getInternalNameForCode(insertvalue)).thenReturn(returnvalue)

    when:
    String value = converter.convertTo(insertvalue, null)

    then:
    value == returnvalue

    where:
    insertvalue                      | returnvalue
    PartnerCodes.Geschlecht.mannlich | "männlich"
    null                             | "NO INTERNAL NAME"
 }
}

和被测方法的实现看起来

public String convertTo (CodeIdentifier source, String destination)
{
  if (source != null)
  {
     return CodeCacheManager.getInternalNameForCode (source);
  }
  return "NO INTERNAL NAME";
}

【问题讨论】:

  • 您找到解决方法了吗?我也有同样的问题
  • 看下面的答案

标签: java gradle powermock jacoco


【解决方案1】:

Jacoco Offline Instrumentation 是解决这个问题的方法。

查看我的 gradle 构建文件

Build and run tests:
Linux:
\$ ./gradlew
Windows:
\$ gradlew
------------------------------------------
"""

apply plugin: 'java'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'

// Project group and version
group 'com.abcd.jacocoTest'
version '1.0.0'

// JDK version source compatibility
sourceCompatibility = 1.8

// JDK version target compatibility
targetCompatibility = 1.8

configurations {
    jacocoAnt
    jacocoRuntime
}

task wrapper(type: Wrapper) {
    gradleVersion = "4.5.1"
}

defaultTasks 'clean', 'test'

buildscript {
    repositories {
        maven { url "https://plugins.gradle.org/m2/" }
    }

    dependencies {
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.2"
    }
}

repositories {
    mavenCentral()
}

dependencies {

    jacocoAnt group: 'org.jacoco', name: 'org.jacoco.ant', version: '0.8.1'
    jacocoAgent group: 'org.jacoco', name: 'org.jacoco.agent', version: '0.8.1'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    testCompile group: 'org.mockito', name: 'mockito-core', version: '2.8.9'
    testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.7.4'
    testCompile group: 'org.powermock', name: 'powermock-api-mockito2', version: '1.7.4'
    testCompile group: 'org.powermock', name: 'powermock-api-easymock', version: '1.7.4'

}

test {
    testLogging {
        afterSuite { desc, result ->
            if (!desc.parent) { // will match the outermost suite
                println "Unit Tests: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
            }
        }
    }

    jacoco {
        append = "false"
        destinationFile = file("$buildDir/reports/jacoco/jacoco-sonar/jacoco-coverage.exec")
    }
}

jacoco {
    toolVersion = "0.8.0"
}

jacocoTestReport {
    reports {
        html.destination file("${buildDir}/reports/jacoco/jacocoHtml")
    }
}

sonarqube {
    properties {
        property "sonar.projectName", 'JacocoTest'
        property "sonar.host.url", "http://localhost:9000"
        property "sonar.java.binaries", "${buildDir}/classes"
        property "sonar.java.libraries", "**/*.jar"
        property "sonar.dynamicAnalysis", "reuseReports"
        property "sonar.jacoco.reportPaths", "${buildDir}/reports/jacoco/jacoco-sonar/jacoco-coverage.exec"
    }
}

task instrument(dependsOn: ['classes']) {
    ext.outputDir = buildDir.path + '/reports/classes-instrumented'
    doLast {
        ant.taskdef(name: 'instrument',
                classname: 'org.jacoco.ant.InstrumentTask',
                classpath: configurations.jacocoAnt.asPath)
        ant.instrument(destdir: outputDir) {
            fileset(dir: sourceSets.main.output.classesDir)
        }
    }
}
gradle.taskGraph.whenReady { graph ->
    if (graph.hasTask(instrument)) {
        tasks.withType(Test) {
            doFirst {
                classpath = files(instrument.outputDir) + classpath + configurations.jacocoRuntime
            }
        }
    }
}
task report(dependsOn: ['instrument', 'test']) {
    doLast {
        ant.taskdef(name: 'report',
                classname: 'org.jacoco.ant.ReportTask',
                classpath: configurations.jacocoAnt.asPath)
        ant.report() {
            executiondata {
                ant.file(file: buildDir.path + '/reports/jacoco/jacoco-sonar/jacoco-coverage.exec')
            }
            structure(name: 'Example') {
                classfiles {
                    fileset(dir: sourceSets.main.output.classesDir)
                }
                sourcefiles {
                    fileset(dir: 'src/main/java')
                }
            }
            html(destdir: buildDir.path + '/reports/jacoco')
        }
    }
}

此处报告任务将创建项目的离线检测文件。

最后,执行 sonarqube 任务。然后你可以看到powermocked类的覆盖范围也包括在内了。

Execution command ./gradlew report sonarqube

然后去你的sonarqube主机(localhost:9000)看看。

【讨论】:

    【解决方案2】:

    PowerMock 和 Jacoco 不兼容,一起使用时不起作用。

    【讨论】:

    • 你改变了什么工具来覆盖单元测试?
    • 我完全放弃了 PowerMock 并开始重新设计我们的软件。由于我们使用的是 Java 8,因此您可以替换例如通过在接口中使用默认方法实现的静态实用程序方法。因此只有 Spock 和 Jacoco。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 1970-01-01
    相关资源
    最近更新 更多