A Complete Guide to Using Clover – A Code Coverage Tool

Quite often we come across situations in the software development industry where we ensure that every single feature is tested before a release, but we still find something broken once the application is released into production. It can be either due to a couple of testers not creating all the test cases required, or missing the execution of a couple of existing tests during the regression cycle. This can be either a manually tested regression suite or an automated suite, we still run into the same problem, wherein the so-called test coverage is 100%, but we still find something broken once released.

To ensure the entire application code has been covered as part of the testing process we can use the Clover code coverage tool. In this blog, I will be focusing on using Maven as the build tool and will be using a sample java spring application. So this is how the tool works.

Basically, when the project is built, all the classes in the project get instrumented and these instrumented classes get packaged as part of the jar/war file. To keep it simple, you can think of instrumentation as the Java classes being slightly modified, to track which lines of the class are being traversed when the application is used. Along with the instrumentation, the clover.db registry is created, where the project structure and the details of all the classes are stored.

Now when you deploy this jar/war file, the instrumented classes are deployed, and the clover coverage recorder files are created. These .db files store the coverage information of which lines of code have been traversed when the application is in use.

Now let us try it out on a sample application.

You can download any public sample application available on Github to try it out. I have downloaded this publicly available application from Github: https://github.com/joakim666/spring-boot-spring-loaded-java8-example

Steps:

1. Add the below profiles section in the pom.xml of this project.

2. Build the application using the below command:

Once the build is done, you will find that an ‘instrument’ folder is created under your project and a ‘clover.db’ registry has been created. If you navigate into the classes folder (‘target\classes\hello’) you will also find that there are additional .class files created with _CLR4 appended. These are the instrumented classes.

3.Run the application using the below command from the project directory:

Once the application is started, you can see that there are additional clover .db files created having names such as ‘clover.db.liverec’, ‘clover.db6ivvjj_lkzicfyg’ etc. These files are used to store the details of which lines of code have been traversed in each of the classes when the user is using the application.

4. Now let us generate a Clover report before we use the application and see what the coverage looks like and what lines of code are covered. To generate a clover report we will first need the clover jar file. You can copy the clover jar from the maven repository ({your home directory}\.m2\repository\org\openclover\clover\4.4.1\clover-4.4.1.jar) and place it in your project directory. Once done you can run the below command.

5. You can now open ‘dashboard.html’ under the ‘CloverReports’ folder to view the coverage details. Here you can see that currently there would be only about 30–35% coverage, which would mainly be the code that was used to bring up the application. You can also see that the methods responsible for displaying the home page, login page etc have not been used yet (Highlighted in red).

6. Now navigate to the home page of the application using the URL http://localhost:8080/home. On the home screen click on the ‘here’ link that is displayed. This will take you to the login screen. Once done, generate the report once again using the command in step 4 and view the coverage information in the Clover report.

You can now see that the coverage percentage has increased from 30% to around 65%. Also, the ‘home’ method responsible for displaying the home screen, and the ‘login’ method responsible for displaying the login screen are shown as covered in your Clover report.

And this is how you can ensure that 100% of all your code has been tested, either manually or through automated capabilities, before releasing an application to the production environment without any margin for human error.

Clover also provides advanced capabilities for excluding specific packages, classes and sections of code such as the catch blocks. It also provides capabilities for capturing coverage in a distributed infrastructure as well.

Leave a Reply

Your email address will not be published.

You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*