2

I'm learning about test coverage and struggling to reach the percentages asked in one of the course projects.

To illustrate the issue, see this repository (picked because they pushed their target folder back then): https://github.com/geo1796/parkingsystem

When it was built 13 months ago, the jacoco report (in target/site/jacoco/index.html) was showing a test coverage of 67% total.

However, if I do mvn clean + mvn test, the report generated shows only 27%

comparison of JaCoCo reports in 2021 and 2022

(I am using IntelliJ, if that matters)

So how do I make it show 67% again? Is this because last year it included the integration tests and now it doesn't? (there is a huge difference in coverage for the DAO classes, for example)

Or is it a bug with JaCoCo?

  • `mvn test` does not run integration tests, try `mvn verify` – Andrey B. Panfilov Jul 07 '22 at 04:20
  • Thank you. That didn't make a difference because JaCoCo only reports coverage from unit tests, but was worth the try. Still at 27% instead of 67% – Ricardo Aranguren Jul 07 '22 at 04:28
  • Oops, I have missed a fact there is no setup of `jacoco-maven-plugin` for `verify` phase. Need also bind `prepare-agent-integration` and `report-integration` goals to `pre-integration-test` and `post-integration-test` phases – Andrey B. Panfilov Jul 07 '22 at 04:38
  • I believe that the original report at 67% was made with only mvn test, which generates the site/jacoco pages. I can't figure out why it shows 27% when I run it now... – Ricardo Aranguren Jul 07 '22 at 04:43

1 Answers1

0

Well, it is hard to investigate what is going wrong in particular project when developers fail to follow best practices, so, what is written below is based on my own opinion about how it should work.

I succeeded to get following jacoco-it:

parking-system
Element Missed Instructions Cov.    Missed Branches Cov.    Missed  Cxty    Missed  Lines   Missed  Methods Missed  Classes
Total   384 of 1,119    65% 41 of 61    32% 48  95  122 315 21  62  4   13
com.parkit.parkingsystem.service    158299  65% 2013    39% 19  34  49  124 4   15  1   3
com.parkit.parkingsystem.config 807 8%  6   0%  7   9   33  35  4   6   0   1
com.parkit.parkingsystem.util   4511    19% 4   0%  5   6   15  17  3   4   0   1
com.parkit.parkingsystem.dao    43319   88% 35  62% 3   13  8   87  0   9   0   2
com.parkit.parkingsystem.model  4084    67% 82  20% 9   27  10  42  5   22  0   2
com.parkit.parkingsystem    12  0%      n/a 3   3   5   5   3   3   1   1
com.parkit.parkingsystem.constants  615 71%     n/a 2   3   2   5   2   3   2   3

after following steps:

  1. bind prepare-agent-integration and report-integration to maven lifecycle:
diff --git a/pom.xml b/pom.xml
index 0bf2fb9..fc3bdaa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -153,6 +153,20 @@
                             <goal>report</goal>
                         </goals>
                     </execution>
+                        <execution>
+                            <id>prepare-agent-integration</id>
+                            <phase>pre-integration-test</phase>
+                            <goals>
+                                <goal>prepare-agent-integration</goal>
+                            </goals>
+                        </execution>
+                        <execution>
+                            <id>post-report-integration</id>
+                            <phase>post-integration-test</phase>
+                            <goals>
+                                <goal>report-integration</goal>
+                            </goals>
+                        </execution>
                 </executions>
             </plugin>
             <plugin>
  1. instantiate mysql db:
% docker run -p 3306:3306 -e MYSQL_ROOT_PASSWORD=rootroot  mysql:latest
% docker exec -ti 61c9c7931a5a /bin/bash
root@61c9c7931a5a:/# su - mysql
$ mysql -u root -p     
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database test
    -> ;
Query OK, 1 row affected (0.01 sec)

mysql> use test;

@execute resources/Data.sql starting from line 5

  1. set JAVA_HOME to 1.8 - it is important because they are using outdated version of jacoco
  2. mvn verify
Andrey B. Panfilov
  • 4,324
  • 2
  • 12
  • 18