2

I used only 'command line' in all of these processes and I want to do that.(not using Ant, maven, gradle, etc.)

I would like to run Junit test case and JaCoCo coverage estimation.

First, below is my code

Calculator.java:

public class Calculator {
  public int evaluate(String expression) {

    int sum = 0;
    for(String summand: expression.split("\\+"))
        sum += Integer.valueOf(summand);
    System.out.println("Hello World!!");
    return sum;
  }
}

CalculatorTest.java:

import static org.junit.Assert.assertEquals;
import org.junit.Test;

public class CalculatorTest {
  @Test
  public void evaluatesExpression(){
    Calculator calculator = new Calculator();
    int sum = calculator.evaluate("1+2+3");
    assertEquals(6, sum);
  }
}

2 java files are in C\Cal and Cal folder contains 4 jar files (junit-4.12.jar, hamcrest-core-1.3.jar, jacococli.jar, jacocoagent.jar)

In this situation, I maked classfile Calculator.class.

javac Calculator.java

And then, I maked exec (I think it is execution file of jacoco) file to estimate unit-test coverage of Calculator.java file.

java -javaagent:jacocoagent.jar=destfile=jacoco.exec

After that, I extracted the report using the exec file and command

java -jar jacococli.jar report jacoco.exec --classfiles Calculator.class --html "report" --name jacocoReport --sourcefiles "Calulator.java"

However, the test coverage in the html report I made was 0%.

Here is my questions:

  1. Do you know why my coverage is 0%?

  2. Is there any problem in trying to extract the coverage result I want?

  3. When calculating the coverage of the Calculator.java file by CalculatorTest.java code and extracting the report, is it correct to write --classfiles only using Calculator.class?

  4. There is no difference in the results even if you insert both Calculator.java and CalculatorTest.java in --sourcefiles. Did I put it wrong? Or what is the effect of --sourcefiles?

And this is the Internet pages I referenced:

3
  • Offline Instrumentation is your keyword. Google it. Commented Mar 27, 2018 at 6:49
  • What are you using to build your project (As in Gradle/ Maven)? Commented Mar 27, 2018 at 6:49
  • I just using 'cmd' on window(like terminal on linux). Commented Apr 1, 2018 at 17:24

1 Answer 1

3

Do you know why my coverage is 0%?

There is no difference in the results even if you insert both Calculator.java and CalculatorTest.java in --sourcefiles. Did I put it wrong? Or what is the effect of --sourcefiles?

--sourcefiles should point on a directory containing packages. And in your case your files use default package (no package), so --sourcefiles should point on directory containing Calculator.java.

Is there any problem in trying to extract the coverage result I want?

No - there is not problem. For example having Calculator.java in directory src and CalculatorTest.java in directory test-src

mkdir classes
javac src/Calculator.java -d classes
mkdir test-classes
javac -cp junit-4.12.jar:classes test-src/CalculatorTest.java -d test-classes
java -javaagent:jacoco-0.8.1/lib/jacocoagent.jar -cp junit-4.12.jar:hamcrest-core-1.3.jar:classes:test-classes org.junit.runner.JUnitCore CalculatorTest
java -jar jacoco-0.8.1/lib/jacococli.jar report jacoco.exec --classfiles classes --sourcefiles src --html report

produce following report

report

When calculating the coverage of the Calculator.java file by CalculatorTest.java code and extracting the report, is it correct to write --classfiles only using Calculator.class?

--classfiles should point on class files, or directories containing class files, or archives containing class files, which should appear in report. CalculatorTest.class was not specified in example above, so it doesn't appear in report. And if specified:

java -jar jacoco-0.8.1/lib/jacococli.jar report jacoco.exec --classfiles classes --sourcefiles src --classfiles test-classes --sourcefiles test-src --html report

report

Sign up to request clarification or add additional context in comments.

4 Comments

I really appreciate your kind reply. However, when I use the 'java -javaagent:jacoco-0.8.1/lib/jacocoagent.jar -cp junit-4.12.jar:hamcrest-core-1.3.jar:classes:test-classes org.junit.runner.JUnitCore CalculatorTest' command, the error indication is "can not find (or load) class 'org.junit.runner.junitcore'
@jeongookjang on Windows class path entries delimited by semicolon ; , i.e. java -javaagent:jacoco-0.8.1/lib/jacocoagent.jar -cp junit-4.12.jar;hamcrest-core-1.3.jar;classes;test-classes org.junit.runner.JUnitCore CalculatorTest
I solved the problem thanks to you. One more question. How do I view the coverage percentage of a line, not the percentage of an instruction's coverage?
@jeongookjang the format of a report is not configurable. If you absolutely need percentage of lines instead of instructions, then divide missed on total. Values can be extracted programmatically from XML and CSV reports. And please don't forget to mark answer as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.