Product
Solutions
Resources
Company
Download Trial Book a Demo

Code Coverage Maven Build for Java Applications

You will learn how to generate a code coverage Maven build for Java applications using the RKTracer tool in 3 simple steps. Prefix the rktracer command to your mvn build, run your tests, and produce an HTML report.

You need to prefix the rktracer command to the mvn build command to enable the code coverage tool. The runtime Jar will be added to the build system by RKTracer. Tests can be run on an RKTracer-instrumented application on a native host or on a server machine, and code coverage can be generated.

The RKTracer tool instruments all the source files compiled in the build process. By default, RKTracer instruments for multi-condition, and you see the following code coverage information in the HTML report:

  • Function Coverage
  • Line Coverage
  • Statement Coverage
  • Multi-Condition Code Coverage
In short

Prefix rktracer to the mvn build command, run your unit or integration tests, then run rkresults to generate the HTML report.

Code Coverage Maven build example

Here is an example application to demonstrate how code coverage can be generated with the Maven build system. For example, if your original build command is:

original mvn command
mvn clean test

Step 1: Enable RKTracer Tool

Prefix the rktracer command (here with the verbose flag -v) to the mvn build command to enable the coverage tool:

with rktracer
rktracer -v mvn clean test

RKTracer automatically adds the runtime library to the Maven installation.

What happens during instrumentation

RKTracer instruments the Java source file with c:\rktracer/lib/librklij-x86_64-w64-mingw32.exe and writes the instrumented file to the prime.java file in the preprocessed directory. Also, it stores the metadata, the structure of instrumentation, and a copy of the source file content in a JSON file. The tool will refer to rktracer.config for internal configuration at the time of instrumentation.

Step 2: Run tests on instrumented application

You can run unit tests or integration tests on a native host machine or server machine. In this case, we will run unit tests.

You can also see coverage metrics written out to the rk-coverage.txt file.

Step 3: Generate Code Coverage HTML reports

Once you have the rk-coverage.txt file, you can run the command rkresults to generate HTML reports in the application working directory. Ensure that you run the rkresults command in the application root folder, or where you have the rktracer folder.

generate the report
rkresults

The rkresults command will search the coverage data file rk-coverage.txt and map it with the JSON files (generated during instrumentation) in the rktracer folder and generate an HTML report. You can manually open the HTML report using index.html.

Enable coverage for selected files

For example, to instrument source files from some folders while ignoring others, add the following to rktracer.config:

rktracer.config
ignore *.java
instrument */power/* */core/* */keys/*
never */firmware_loader/* */lockdown/*

To generate coverage only for specific functions in specific files, edit rktracer.config:

rktracer.config
ignore *.java
instrument *source-file-X.java *source-file-Y.java *source-file-Z.java
function-ignore *
function-instrument fun_X() fun_Y() fun_Z()

Next steps

Once your Maven project reports coverage, the same RKTracer workflow applies across other build systems and IDEs. Browse the rest of the documentation, or read more about how RKTracer measures coverage.

Stuck on your build?

Open a support ticket and an engineer who knows your toolchain will help. You get a tracked ticket ID by email.