简体   繁体   English

从jar执行中获取Java知识

[英]Getting Java coverage from jar execution

I have a JAVA application without unit tests (I know, inherited code), and I need to get the coverage from several tests which are defined as executing the jar file with some presets. 我有一个没有单元测试(我知道,继承的代码)的JAVA应用程序,我需要从几个测试中获得覆盖,这些测试定义为使用一些预设执行jar文件。

I used to do it with Emma from console like this: 我以前是通过控制台通过Emma来做到这一点的:

java -cp emma.jar emmarun -raw -ix +some.filter.* -out ./coverage/test001.em -jar MyJarFile.jar <application arguments>

The problem is the filtering with -ix argument. 问题是使用-ix参数进行过滤。 I wanted to get coverage only from my own packages, which are named com.mycompany.whatever.blah.blah.blah . 我只想从我自己的软件包(即com.mycompany.whatever.blah.blah.blah获得覆盖。 Therefore, by inclussion filter is supposed to be just +com.mycompanyname.* . 因此,按包含过滤器应该只是+com.mycompanyname.* Nevertheless, I only get about half of the packages, and not even every class into each package. 尽管如此,我只得到了大约一半的软件包,甚至没有每个类都进入每个软件包。

I also tried other combinations, such as using only exclussion patterns to avoid all the non desirable packages, and the result is the same. 我还尝试了其他组合,例如仅使用排他模式来避免所有不受欢迎的程序包,结果是相同的。 I known I manages to do it somehow in the past, but I am afraid I am unable to remember how. 我知道我过去曾经设法做到这一点,但恐怕我不记得该怎么做。

So that is why I am looking for a different coverage library which allows me to get coverage from jar files. 因此,这就是为什么我要寻找一个不同的coverage库,该库允许我从jar文件中获取coverage。 I saw JMockit, Jacoco and even the impressing Sonar, but it seems that they don't allow jar-coverage and that they are designed for unit testing coverage. 我看到了JMockit,Jacoco甚至是令人印象深刻的Sonar,但似乎它们不允许罐子覆盖,它们是为单元测试覆盖而设计的。

Any suggestion? 有什么建议吗?

JaCoCo includes an agent called jacocoagent.jar which does the job. JaCoCo包含一个名为jacocoagent.jar的代理来完成该工作。 I could not find examples online, but I tried following the documentation and this works: 我无法在线找到示例,但是我尝试按照文档进行操作,并且可以正常工作:

java -javaagent:./route/to/jacocoagent.jar=destfile=./coverage/test001.exec,includes=com.mycompany.* -jar MyJarFile.jar <Jar arguments>

Then I can load the exec files into Eclipse with the eclemma pluggin, merge them, and export as a HTML file. 然后,我可以使用eclemma插件将exec文件加载到Eclipse中,进行合并,然后将其导出为HTML文件。 I think it is possible to generate a HTML report from ant/maven or even with a Java application written for that purpose (jacoco developers provide an example), but I haven't tested those options yet. 我认为可以从ant / maven生成HTML报告,甚至可以使用为此目的编写的Java应用程序生成报告(jacoco开发人员提供了一个示例),但是我尚未测试这些选项。

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

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