简体   繁体   中英

java coverage is 0% with Junit & Jacoco from the command line

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:

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

报告

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

报告

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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