how to create a jacoco.exec by executing standalone junit against a java jar application

886 views
Skip to first unread message

joysha...@gmail.com

unread,
Oct 29, 2018, 4:56:32 AM10/29/18
to JaCoCo and EclEmma Users
hi, i have created a customerorder application(not a web application) and packaged it in main.jar.

I want to use the jacoco CLI introduced in 0.8 version..

Step 1. I created the instrumented classes by executing
java -jar jacococli.jar instrument <dir where all my application are> --dest C:\jacoco\target
here, C:\jacoco\target is the directory where the instrumented classes will placed.

Step2: I ran my junit test class as standalone application::
java -cp ${CLASSPATH} org.junit.runner.JUnitCore com.samples.customerinfo.CustomerOrderInfoTest

Junit sucessfull done!!

Step3:: Missing link.. How my executed junit will create the jacoco.exec files, i want to use the CLI options of jacoco library..

do i need to pass the jacocoagent.jar in the JVM , i am using powershell only.

Pl. suggest..

joysha...@gmail.com

unread,
Oct 29, 2018, 5:06:17 AM10/29/18
to JaCoCo and EclEmma Users


Hi All,
I found the reply..
Step 2 needs to be changed and javaagent options needs to added and pointing to jacocoagent.jar

java -javaagent:jacocoagent.jar=destfile=jacoco.exec -cp ${CLASSPATH} org.junit.runner.JUnitCore com.samples.customerinfo.CustomerOrderInfoTest

Evgeny Mandrikov

unread,
Oct 29, 2018, 5:09:56 AM10/29/18
to JaCoCo and EclEmma Users
If you use agent, then you don't need to instrument classes in step 1.

joysha...@gmail.com

unread,
Oct 29, 2018, 5:41:02 AM10/29/18
to JaCoCo and EclEmma Users
ok.. so is there any other way to create the jacoco.exec file if i am not using jacocagent while executing the junit from command line..

Evgeny Mandrikov

unread,
Oct 29, 2018, 6:48:03 AM10/29/18
to JaCoCo and EclEmma Users
On Monday, October 29, 2018 at 10:41:02 AM UTC+1, joysha...@gmail.com wrote:
ok.. so is there any other way to create the jacoco.exec file if i am not using jacocagent while executing the junit from command line..

Execution of JUnit with coverage is not different from execution of any other Java application with coverage, so for simplicity I will demonstrate last one.

Given "src/Example.java":

class Example {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

Compile:

$ mkdir classes
$ javac -g src/Example.java -d classes

Then you have two distinct options.

First and recommended - on-the-fly instrumentation with agent:

$ java -cp classes -javaagent:jacoco-0.8.2/lib/jacocoagent.jar Example
Hello, World!

Second - offline pre-instrumentation without agent:

$ java -jar jacoco-0.8.2/lib/jacococli.jar instrument classes --dest instrumented
[INFO] 1 classes instrumented to /private/tmp/example/instrumented.

$ java -cp instrumented:jacoco-0.8.2/lib/jacocoagent.jar Example
Hello, World!

In both cases by default "jacoco.exec" is created in current directory.
About customizations please read:
or
respectively.

Generation of report using CLI:

$ java -jar jacoco-0.8.2/lib/jacococli.jar report jacoco.exec --classfiles classes --sourcefiles src --html report
[INFO] Loading execution data file /private/tmp/example/jacoco.exec.
[INFO] Analyzing 1 classes.

joysha...@gmail.com

unread,
Oct 29, 2018, 7:27:29 AM10/29/18
to JaCoCo and EclEmma Users
Thank you so much.. If this is added in jacoco CLI documentation, it would be very helpful !!
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages