4

I have a multi-module java spring boot project. Here is my project structure

- pom.xml (root)
- war (project)
--- pom.xml
--- src/.../SBMainApplication.java
- api (project)
--- pom.xml
--- src/../Controller1.java
--- test/unit/
--- test/integration
- service (project)
--- pom.xml
--- src/../Service1.java
--- test/unit/
- domain(project)
--- pom.xml
--- src/../Repository1.java
--- test/unit/

As you can see, there are 4 modules and finally when war module is built it pakages other modules as a Spring Boot jar.

I have enabled jacoco for code coverage and it works fine for all unit tests, which are there in each module. The integration tests however only exist in api module. My expectation is if I am running a SpringRunner integration on API and the flow is Controller1 -> Serivice1 -> Repository1 -> h2 database, the integration tests would cover all the classes in other modules too. But in the jacoco coverage only the controllers are being covered but the services and repositories are not covered. Here is my jacoco configuration in the root pom

 <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco.version}</version>
                <configuration>
                    <excludes>
                        <exclude>com/../model/**/*</exclude>
                        <exclude>com/../common/model/**/*</exclude>
                        <exclude>com/../repository/entity/**/*</exclude>
                        <exclude>**/*Constants.java</exclude>
                        <exclude>**/*Configuration.java</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-initialize-ut</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-initialize-it</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>PACKAGE</element>
                                    <excludes>
                                           <exclude>com/../model/**/*</exclude>
                                           <exclude>com/../common/model/**/*</exclude>
                                           <exclude>com/../repository/entity/**/*</exclude>
                                           <exclude>**/*Constants.java</exclude>
                                           <exclude>**/*Configuration.java</exclude>
                                    </excludes>
                                    <limits>
                                        <limit>
                                            <counter>LINE</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0%</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                    <execution>
                        <id>jacoco-report</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <excludes>
                        <exclude>**/*IntTest.java</exclude>
                    </excludes>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*IntTest.java</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

Is there a way where service and repositories are covered in jacoco by running the integration tests in another module?

Tatha
  • 1,253
  • 2
  • 24
  • 42
  • 1
    hello, did you find a solution to this issue? I'm having the same problem – raffah Jul 23 '20 at 11:25
  • 1
    @raffah yes I did, followed the example https://github.com/jacoco/jacoco/tree/master/jacoco-maven-plugin.test/it/it-report-aggregate. Basically created another module called report and added an aggregate-report in there. In the parent pom added the property for sonar to take it from aggregate report ${project.basedir}/../report/target/site/jacoco-aggregate/jacoco.xml – Tatha Jul 25 '20 at 20:20
  • 2
    For anyone still having the same issue, I've found this post: https://natritmeyer.com/howto/reporting-aggregated-unit-and-integration-test-coverage-with-jacoco/ You can basically define jacoco configurations in the parent pom and it will generate merged reports (unit+it) for each module. – emrekgn Mar 30 '21 at 14:51

0 Answers0