The JaCoCo plugin for Maven only reports code coverage for tests run in a particular module rather than reporting coverage across modules. This can be an issue for multi-module projects where unit tests from one module test code in other modules. Some projects are set up with unit tests in a separate module all by themselves, making the JaCoCo code coverage reports nearly useless. No matter what setup you run, JaCoCo won’t report code coverage well for integration tests that run across modules.There are a number of possible solutions to address this challenge. One of the most obvious solutions is to avoid having a separate module for unit tests, rather including unit tests in the appropriate module. That probably won’t get you the whole way and may be a significant refactoring project.Maven users can also create a separate reporting module with dependencies. Use this module to create dependencies to all the modules you need for your coverage report. Make sure the module has access to the right files, and accurate location paths are included.You can use an additional tool to make up for this deficit. Some possible solutions are SonarQube, Jenkins, and Arquillian.Since the Maven JaCoCo plugin doesn’t support merging of subproject metrics into a single report file, a possible workaround solution is to use Ant with Maven. The task objectives with this approach can be summarized as follows:
- Add JaCoCo plugin to the Parent POM. If specific modules don’t have to be included in the test coverage, change the plugin definition based on specific projects as required.
- Create a submodule to append all results from JaCoCo using the Ant task.
- Specify the Ant command from Maven using the Maven Antrun plugin.
- Configure the JaCoCo Ant task configuration process with the necessary information such as location of build, class, source or generated-source directories.
- From the Antrun plugin, define the reporting tasks. Specify locations to store the JaCoCo report execution data. This information can be covered for each submodule within each project.
- Create a reporting structure based on requirements, such that each group of elements will contain information only on the specific set of modules.
Hopefully, this helps you find some solutions (or at least workarounds) to the multi-module code coverage reporting when using Maven and JaCoCo. By the end of the day, while testing teams like to aim for 100 percent test coverage, it’s also important to measure it and avoid unnecessary testing. This concern is even more critical for handling multiple modules in test coverage reporting. Make sure your Maven tool handles all the necessary conditions and paths nicely, thereby accurately ensuring 100 percent test coverage across multi-module projects.