samedi 8 février 2020

Generating JaCoCo coverage report by executing JAR application

I am developing a console Java application (CLI) and trying to write some black-box tests by executing this program, checking output and comparing it with the expected (using JUnit). Everything is ok, but I can't get the code coverage report from JaCoCo.

JaCoCo and JUnit configuration in my build.gradle file:

...

plugins {
    id 'jacoco'
}

...

dependencies {
    ...

    testCompile('org.junit.jupiter:junit-jupiter:5.5.2')
}

test {
    useJUnitPlatform()
}

jacoco {
    toolVersion = "0.8.5"
    reportsDir = file("$buildDir/reports")
}

jacocoTestReport {
    reports {
        xml.enabled true
        csv.enabled false
        xml.destination file("${buildDir}/coverage.xml")
    }

    getExecutionData().setFrom("$buildDir/jacoco/test.exec")
}

In tests I do this:

String command = "java -javaagent:libs/jacocoagent.jar=destfile=build/jacoco/test.exec -jar build/libs/myapp.jar";
Process process = Runtime.getRuntime().exec(command);
process.waitFor();

InputStream inputStream = process.getInputStream();

byte[] b = new byte[inputStream.available()];

String cliOutput = new String(b);

After Test executing files build/jacoco/test.exec and build/coverage.xml are created, but doesn't contain any coverage information.

For simple Unit tests coverage is generating, but it not including coverage from executing the entire program as above.

Aucun commentaire:

Enregistrer un commentaire