1

I have a setup for multi Module module project something like this

Module1

|
 submodule1
|
submodule2

I have written a Junit test in submodule 1 and it's covering the code of submodule 2 also but when I try to see coverage in sonar it's showing 0% for submodule 2 is there any way to show coverage of submodule2 ? Also I can generate aggregated xml report by running mvn clean install jacoco:report-aggregate but how can I feed this aggregated report into sonar ? How to setup pom of module as well as submodules ?

  • 1
    How have you called Sonar ? Maven ? From where have you called Maven? – khmarbaise Dec 14 '20 at 14:03
  • Please refer to [creating a parent pom.](https://stackoverflow.com/questions/39002511/how-to-create-maven-parent-pom-speciific-to-organization) That should help you further. – Samy Dec 14 '20 at 14:56

1 Answers1

0

I'm not sure if this is what you are doing, but I'll say it anyway. If you are creating submodule1 as 'code' and submodule2 as 'tests', that's a horrible way to organize it. Tests (specifically, unit tests) should be stored with the code that it is testing. Thus, each submodule has src/main/java and src/test/java.

Now, that said, it is plausible that you have a 'common-lib' module, and then additional modules that depend on that lib (e.g. application tier). It is highly likely that you run a test at the application-tier and it invokes code in the lib module. But you get no code-coverage credit for that. Jacoco coverage of the lib will be solely based on the tests in the lib. The 'app' tests only give you coverage of the app. This has to do with the instrumentation that happens when jacoco runs - it's only going to instrument the things local to the module it is running in.

Yes, there is a jacoco-aggregate report, but this will merge the module reports together - single report of each module. It does NOT give you unified coverage of an app-tier test calling lib methods, etc.

Lastly, sonar. I believe as long as you have jacoco files hanging around, the sonar-scanner will make use of them. I think it operates on the raw jacoco.exec file, but may be able to interpret a jacoco-result.xml or jacoco-aggregate.xml. If it is giving you grief, include your pom.xml

Jeff Bennett
  • 996
  • 7
  • 18
  • Being JaCoCo developer, can't resist from correcting: By default JaCoCo agent instruments all classes loaded in JVM regardless from which on-disk location (i.e. Maven module) they are loaded. And thus [`jacoco:report-aggregate`](https://www.eclemma.org/jacoco/trunk/doc/report-aggregate-mojo.html) can provide "unified" coverage of classes of one Maven module due to calls from classes of another module. – Godin Dec 15 '20 at 05:55