How to check code coverage with JaCoCo agent?

user3450486

I am going to deploy application with JaCoCo agent to production environment to let it work for some time. The result should help me identify the parts of code I can get rid of.

I started some research around the topic and prepared HelloWorld application:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello world");
    }
}

Then I compiled the class: "javac HelloWorld.java" and got HelloWorld.class file.

Now I run the app with the following command: "java -javaagent:jacocoagent.jar HelloWorld" the program executes and jacoco binary is generated. The file contains some binary data.

Everything looks fine but the coverage report shows 0% coverage although it should be 100%.

Has anyone faced this issue or correct me what I am doing the bad way?

bhdrkn

I generated full report using this steps. Since I use maven for this kind of operations I added maven after your steps. I created HelloWorld.java just copying from your question. Then I follow these steps:

javac HelloWorld.java which outputs HelloWorld.class Then I created jacoco.exec by executing java -javaagent:jacocoagent.jar HelloWorld

Then I created a pom.xml file which contents are like this.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>test</name>


    <build>
        <plugins>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.7.5.201505241946</version>
            </plugin>
        </plugins>
    </build>

After that I created a target/classes directory. I copied jacoco.exec to target/ and HelloWorld.class to target/classes.

Then I executed mvn jacoco:report which generates a report to target/site/jacoco. Which contains correct coverage information.

I know using maven may not sound good for a simple application. But I don't know any other way to generate reports from jacoco.exec. By the way your maven plugin version and jacocoagent version must match.

And here the result I get.result

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to exclude a line from jacoco code coverage?

From Dev

How to check minimum code coverage for multi-module maven project with jacoco?

From Dev

Jacoco 0% Code Coverage

From Dev

java code coverage with jacoco

From Dev

Jacoco 0% Code Coverage

From Dev

How to check rspec code coverage

From Dev

How can i run Jacoco in android studio for code coverage

From Dev

Jacoco coverage of unit test code

From Dev

Jacoco Code Coverage in android studio

From Dev

Code coverage with jacoco for a Android library

From Dev

Jacoco code coverage is affected by AspectJ

From Dev

Missing Jacoco Code Coverage and IncompatibleClassChangeError

From Dev

Android - Jacoco code coverage ignores Robolectric tests

From Dev

maven jacoco: not generating code coverage report

From Dev

Gradle - Jacoco code coverage without running connectedCheck

From Dev

Sonar maven jacoco code coverage for Multimodule project

From Dev

Why might Jacoco be showing a code coverage of 0?

From Dev

Minimum code coverage threshold in Jacoco Gradle

From Dev

Jacoco: Find code coverage for external tests

From Dev

Android - Jacoco code coverage ignores Robolectric tests

From Dev

Gradle - Jacoco code coverage without running connectedCheck

From Dev

Jacoco code coverage with Arquillian in Multi module project

From Dev

How to report Jacoco Groovy code coverage to Sonar using new Gradle SonarQube plugin?

From Dev

How can I exclude files of the Code Coverage of SonarQube using JaCoCo maven plugin

From Dev

Gradle jacoco multi project. How to output the code coverage of all the project in the terminal?

From Dev

How would I add an annotation to exclude a method from a jacoco code coverage report?

From Dev

How to implement jacoco code coverage in Spock unit testing for generic 'Exception' catch block?

From Dev

How to exclude a class from Jacoco coverage?

From Dev

How to generate jacoco coverage in manual tests?

Related Related

  1. 1

    How to exclude a line from jacoco code coverage?

  2. 2

    How to check minimum code coverage for multi-module maven project with jacoco?

  3. 3

    Jacoco 0% Code Coverage

  4. 4

    java code coverage with jacoco

  5. 5

    Jacoco 0% Code Coverage

  6. 6

    How to check rspec code coverage

  7. 7

    How can i run Jacoco in android studio for code coverage

  8. 8

    Jacoco coverage of unit test code

  9. 9

    Jacoco Code Coverage in android studio

  10. 10

    Code coverage with jacoco for a Android library

  11. 11

    Jacoco code coverage is affected by AspectJ

  12. 12

    Missing Jacoco Code Coverage and IncompatibleClassChangeError

  13. 13

    Android - Jacoco code coverage ignores Robolectric tests

  14. 14

    maven jacoco: not generating code coverage report

  15. 15

    Gradle - Jacoco code coverage without running connectedCheck

  16. 16

    Sonar maven jacoco code coverage for Multimodule project

  17. 17

    Why might Jacoco be showing a code coverage of 0?

  18. 18

    Minimum code coverage threshold in Jacoco Gradle

  19. 19

    Jacoco: Find code coverage for external tests

  20. 20

    Android - Jacoco code coverage ignores Robolectric tests

  21. 21

    Gradle - Jacoco code coverage without running connectedCheck

  22. 22

    Jacoco code coverage with Arquillian in Multi module project

  23. 23

    How to report Jacoco Groovy code coverage to Sonar using new Gradle SonarQube plugin?

  24. 24

    How can I exclude files of the Code Coverage of SonarQube using JaCoCo maven plugin

  25. 25

    Gradle jacoco multi project. How to output the code coverage of all the project in the terminal?

  26. 26

    How would I add an annotation to exclude a method from a jacoco code coverage report?

  27. 27

    How to implement jacoco code coverage in Spock unit testing for generic 'Exception' catch block?

  28. 28

    How to exclude a class from Jacoco coverage?

  29. 29

    How to generate jacoco coverage in manual tests?

HotTag

Archive