Java 覆盖范围不适用于使用gradle的PowerMock、Jacoco和Sonarqube
我已经有一段时间一直在用我的头来打击它了,所以我想我应该把它打开,让别人指出我错在哪里 我使用的是Gradle4.10、Java9.04、Jacoco0.8.4和Sonarqube7.9。我一直试图让我的覆盖范围包括使用Powermock运行的测试,而我的离线资源遇到了一些麻烦。我尝试了多种来源,包括:Java 覆盖范围不适用于使用gradle的PowerMock、Jacoco和Sonarqube,java,gradle,sonarqube,code-coverage,jacoco,Java,Gradle,Sonarqube,Code Coverage,Jacoco,我已经有一段时间一直在用我的头来打击它了,所以我想我应该把它打开,让别人指出我错在哪里 我使用的是Gradle4.10、Java9.04、Jacoco0.8.4和Sonarqube7.9。我一直试图让我的覆盖范围包括使用Powermock运行的测试,而我的离线资源遇到了一些麻烦。我尝试了多种来源,包括: 我对示例所做的更改主要是围绕着对sourceset.main.output.classesDir的弃用。我没有承诺使用任何插件版本,但我现在需要坚持使用Java9和Maven4。如
public class App {
public void mockTest() throws ClassNotFoundException {
try (FileInputStream fis = new FileInputStream(new File("Hello.txt"))) {
//do stuff
} catch (IOException e) {
throw new ClassNotFoundException();
}
}
public String simpleMethod(){
return "Hello world";
}
}
简单应用测试
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"javax.*", "com.sun.*"})
@PrepareForTest({App.class})
public class AppTest {
@Test(expected = ClassNotFoundException.class)
public void testAppHasAGreeting() throws Exception {
PowerMockito.whenNew(FileInputStream.class).withParameterTypes(File.class).withArguments(any(File.class)).thenThrow(IOException.class);
App app = new App();
app.mockTest();
}
@Test
public void simpleMethod() {
SimpleClass simpleClass = new SimpleClass();
Assert.assertEquals("Hello world", simpleClass.simpleMethod());
}
}
渐变构建
plugins {
id "org.sonarqube" version "2.8" apply false
}
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'maven'
apply plugin: 'org.sonarqube'
version = "1.0-SNAPSHOT"
mainClassName = 'App'
configurations {
jacocoAnt
jacocoRuntime
}
dependencies {
jacocoAnt group: 'org.jacoco', name: 'org.jacoco.ant', version: '0.8.5'
jacocoRuntime group: 'org.jacoco', name: 'org.jacoco.agent', version: '0.8.5'
<snip>
compile 'junit:junit:4.12'
}
sonarqube {
properties {
property "sonar.host.url", "http://192.168.159.132:8051/"
property "sonar.login", "11b80d093e28669fe5c31e17553b08258a91016f"
property "sonar.java.binaries", "${buildDir}/classes"
property "sonar.java.libraries", "**/*.jar"
property "sonar.dynamicAnalysis", "reuseReports"
property "sonar.jacoco.reportPaths", "${buildDir}/jacoco/test.exec"
property "sonar.coverage.jacoco.xmlReportPaths", "${buildDir}/test-results/test"
}
}
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) {
for(File f : sourceSets.main.output.getClassesDirs()){
fileset(dir: f)
}
}
}
}
gradle.taskGraph.whenReady { graph ->
if (graph.hasTask(instrument)) {
tasks.withType(Test) {
doFirst {
systemProperty 'jacoco-agent.destfile', buildDir.path + '/jacoco/tests.exec'
classpath = files(instrument.outputDir) + classpath + configurations.jacocoRuntime
}
}
}
}
task report(dependsOn: ['instrument', 'test']) {
println "Source Classes" + sourceSets.main.output.getClassesDirs()
doLast {
ant.taskdef(name: 'report',
classname: 'org.jacoco.ant.ReportTask',
classpath: configurations.jacocoAnt.asPath)
ant.report() {
executiondata {
ant.file(file: buildDir.path + '/jacoco/test.exec')
}
structure(name: 'Example') {
classfiles {
fileset(dir: "build/classes/")
}
sourcefiles {
fileset(dir: 'src/main/java')
}
}
html(destdir: buildDir.path + '/reports/jacoco')
}
}
}
[ant:report] Classes in bundle 'Example' do no match with execution data. For report generation the same class files must be used as at runtime.
[ant:report] Execution data for class App does not match.
[ant:report] Execution data for class AppTest does not match.