![](/img/trans.png)
[英]Unit tests coverage using Jacoco for test classes written using Powermock
[英]Gradle Jacoco does not track the coverage of Spock test when using Powermock
我正在使用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";
}
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')
}
}
}
此處的報告任務將創建項目的脫機檢測文件。
最后,執行聲納任務 。 然后您可以看到還包括了powermocked類的內容。
Execution command ./gradlew report sonarqube
然后去看看你的聲納主機(本地主機:9000)。
PowerMock和Jacoco不兼容,並且不能同時使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.