Code coverage - XLRIT/gears GitHub Wiki

It is very useful to know which parts of the source code you have executed (e.g. when running tests). This document shows how you can run code coverage analysis while using or testing your application.

Pre-requisites

  1. Download and unzip JaCoCo somewhere on your computer (in this example we will use c:\apps). _For this example we have used version _
  2. Generate and build your application (as described in Home).

Start coverage analysis

In this document we are assuming Windows 10 as your operating system. It should however work similar on other operating systems.

  1. Open a command prompt
  2. Go to your project folder. In this example we are using C:\git\some_client\some_tool as an example folder.
  3. Execute the following commands:
set JACOCO_HOME=c:\apps
set PROJECT=some_tool
java -javaagent:%JACOCO_HOME%\jacocoagent.jar=destfile=target\jacoco.exec -jar target\%PROJECT%\target\%PROJECT%.jar

This will start the generated/build application but will also attach the JaCoCo agent to it, which will do the analysis

Load data, run tests or simply use the application

  1. Load test data (as described in Home).
  2. Run test scenario's (as described in Home).
  3. Or use the application in any way you like

By simply running test scenario's and using the application the JaCoCo agent is seeing which parts of the source code is actually being covered. It will create a file with that coverage data. In this example we have told the JaCoCo agent to create a file target\jacoco.exec.

  1. Go back to the command prompt where you started the application with the JaCoCo agent and press CTRL+C.

This will terminate the application.

Create and view code coverage report

  1. From the same command prompt execute the following command:
java -jar %JACOCO_HOME%\jacococli.jar report target\jacoco.exec --html target\codecoverage --sourcefiles target\%PROJECT%\src\main\java --classfiles target\%PROJECT%\target\classes
  1. Open file target\codecoverage\index.html.

This should open up your default browser and you can now browse the code coverage reports. The opening page should look a bit like this:

By browsing and clicking around you can get more information about your code coverage. At the lowest level you can even show the actual (Java) source code which shows which parts are covered and which parts aren't.

Here is a little explanation:

Item Explanation
Green lines Fully covered.
Red lines Not covered at all. Ideally you should have no red lines.
Yellow lines Covered but not all branch dictating values are covered. E.g. in line 263 only the true value of leaseTypeEqualsOl was covered and not the false value.
Green diamond A branch location where all branches are covered.
Yellow diamond A branch location where some branches are covered but not all.
⚠️ **GitHub.com Fallback** ⚠️