Home » DevOps » Cobertura Tutorial

About Iulian Timis

Avatar photo
My name is Iulian and I graduated Faculty of Electronics, Telecommunications, and Information Technology. My master's degree was in Computer Science, Information Security. I am passionate about technology and the security area. During my career I was working as a Java Developer, on the backend and frontend side, sometimes doing some DevOps tasks.

Cobertura Tutorial

1. Introduction

Every time you write code, you need to write tests to cover at least your code. For writing tests, you have to add some libraries like JUnit or other libraries in this way. In generating coverage code reports, you need to add additional dependencies like JaCoCo, Jcov, or Cobertura.

For the purpose of this article, we will cover the Cobertura tool which will help you to generate the code coverage for your tests. This is a free Java tool that calculates the percentage of code accessed by tests.

The official website for this tool you can find here.

2. Maven Configuration for Cobertura

If you want to integrate this plugin into your maven project you can add it to your maven configuration.

...
<build>
...
    <plugins>
...
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.7</version>
            <configuration>
                <formats>
                    <format>html</format>
                    <format>xml</format>
                </formats>
                <aggregate>true</aggregate>
            </configuration>
            <executions>
                <execution>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>check</goal>
                        <goal>cobertura</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
...
    </plugins>
...
</build>
...

After you added this plugin, you will be able to see Cobertura tasks in the plugins.

Cobertura tasks in the maven project
Fig.1: Cobertura tasks in the maven project

3. Installing Cobertura plugin in Jenkins

To be able to see the coverage in the Jenkins jobs you will have first to configure your maven project to support this feature. This can be done with profiles in the maven configuration.

<project ...>
    ...
    <profiles>
        <!-- Jenkins by default defines a property BUILD_NUMBER which is used to enable the profile. -->
        <profile>
            <id>jenkins</id>
            <activation>
                <property>
                    <name>env.BUILD_NUMBER</name>
                </property>
            </activation>
            <build>
                <pluginManagement>
                    <plugins>
                        <plugin>
                            <groupId>org.codehaus.mojo</groupId>
                            <artifactId>cobertura-maven-plugin</artifactId>
                            <version>2.2</version>
                            <configuration>
                                <formats>
                                    <format>xml</format>
                                </formats>
                            </configuration>
                            <executions>
                                <execution>
                                    <phase>package</phase>
                                    <goals>
                                        <goal>cobertura</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </pluginManagement>
            </build>
        </profile>
    </profiles>
    ...
</project>

The other way around is installing Cobertura plugin in your Jenkins instance. To install the plugin, login into Jenkins, go to the Manage Jenkins tab and then go to the Plugin Manager. From the plugin manager, go to the Available tab and type Cobertura.

Cobertura plugin in the Jenkins
Fig.2: Cobertura plugin in the Jenkins

Furthermore, you can find more information on the official website of the Jenkins plugins.

4. Generating Code Coverage Reports with Maven

You can generate code coverage reports by just calling some tasks from Maven. With this maven plugin, you will have multiple tasks to generate test reports. For the purpose of this demo, we will show you the Cobertura task shown in the previous section.

To generate your tests report just simple run: mvn cobertura:cobertura in your project.

Cobertura reports generated
Fig. 3: Cobertura reports generated

Basically, it will show all the packages where the tests covered that part of that code.

5. Conclusion

Writing tests and generating reports are mandatory on some projects. With this article, we cover most of the important things that you need to configure test reports with Cobertura. Besides that, we introduced you to the Maven dependencies and Jenkins configuration.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments