简体   繁体   English

如何使用JaCoCo代理检查代码覆盖率?

[英]How to check code coverage with JaCoCo agent?

I am going to deploy application with JaCoCo agent to production environment to let it work for some time. 我将使用JaCoCo代理将应用程序部署到生产环境中,以使其工作一段时间。 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: 我围绕该主题开始了一些研究,并准备了HelloWorld应用程序:

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. 然后,我编译了类:“ javac HelloWorld.java”,并获得了HelloWorld.class文件。

Now I run the app with the following command: "java -javaagent:jacocoagent.jar HelloWorld" the program executes and jacoco binary is generated. 现在,我使用以下命令运行该应用程序:“ java -javaagent:jacocoagent.jar HelloWorld”该程序执行并生成了jacoco二进制文件。 The file contains some binary data. 该文件包含一些二进制数据。

Everything looks fine but the coverage report shows 0% coverage although it should be 100%. 一切看起来不错,但覆盖率报告显示0%的覆盖率,尽管应该为100%。

Has anyone faced this issue or correct me what I am doing the bad way? 有没有人遇到这个问题或纠正我在做不好的事情吗?

I generated full report using this steps. 我使用此步骤生成了完整的报告。 Since I use maven for this kind of operations I added maven after your steps. 由于我使用maven进行此类操作,因此在执行步骤之后添加了maven。 I created HelloWorld.java just copying from your question. 我创建的HelloWorld.java只是从您的问题中复制而来。 Then I follow these steps: 然后,我按照以下步骤操作:

javac HelloWorld.java which outputs HelloWorld.class Then I created jacoco.exec by executing java -javaagent:jacocoagent.jar HelloWorld 输出HelloWorld.class javac HelloWorld.java然后,我通过执行java -javaagent:jacocoagent.jar HelloWorld创建了jacoco.exec

Then I created a pom.xml file which contents are like this. 然后,我创建了一个pom.xml文件,其内容如下。

<?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. 之后,我创建了一个target/classes目录。 I copied jacoco.exec to target/ and HelloWorld.class to target/classes . 我将jacoco.exec复制到target/并将HelloWorld.class复制到target/classes

Then I executed mvn jacoco:report which generates a report to target/site/jacoco . 然后我执行了mvn jacoco:report ,该报告生成到target/site/jacoco的报告。 Which contains correct coverage information. 其中包含正确的覆盖范围信息。

I know using maven may not sound good for a simple application. 我知道使用maven对于简单的应用程序可能听起来不太好。 But I don't know any other way to generate reports from jacoco.exec. 但是我不知道从jacoco.exec生成报告的任何其他方法。 By the way your maven plugin version and jacocoagent version must match. 顺便说一下,您的maven插件版本和jacocoagent版本必须匹配。

And here the result I get. 这就是我得到的结果。 结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM