Reader small image

You're reading from  Mastering Unit Testing Using Mockito and JUnit

Product typeBook
Published inJul 2014
Publisher
ISBN-139781783982509
Edition1st Edition
Tools
Right arrow
Author (1)
Sujoy Acharya
Sujoy Acharya
author image
Sujoy Acharya

Sujoy Acharya works as a Principal Engineer with Cerner. While growing up, he pursued his interests in the fields of computer science and engineering. His hobbies are watching movies and sitcoms, playing outdoor sports, and reading books. Sujoy likes to research upcoming technologies. His major contributions are in the fields of TDD, building scalable applications, cloud services, and the Spring Framework. He has authored four books for Packt, namely, Test-Driven Development with Mockito, Mastering Unit Testing using Mockito and JUnit, Mockito Essentials, and Mockito for Spring.
Read more about Sujoy Acharya

Right arrow

Chapter 5. Exploring Code Coverage

This chapter explains the code coverage, coverage tools, and provides step-by-step guidance to generate a coverage report.

The following topics are covered in this chapter:

  • Code, branch, and line coverage

  • Coverage tools such as Clover, Cobertura, EclEmma, and JaCoCo

  • Measuring coverage using Eclipse plugins

  • Using Ant, Maven, and Gradle to generate reports

Understanding code coverage


Code coverage is a measurement of percentage of instructions of code being executed while the automated tests are running.

A piece of code with high code coverage implies that the code has been thoroughly unit tested and has a lower chance of containing bugs than code with a low code coverage. You should concentrate on writing meaningful (business logic) unit tests and not on having 100 percent coverage because it's easy to cheat and have 100 percent coverage with completely useless tests.

Numerous metrics can be used to measure the code coverage. The following are the ones that are widely used:

  • Statement or line coverage: This measures the statements or lines being covered

  • Branch coverage: This measures the percentage of each branch of each control structure, such as the if else and switch case statements

  • Function or method coverage: This measures the function execution

The following Java code will elucidate the metrics.

An absSum method takes two integer arguments...

Configuring the Eclipse plugin


We learned that the coverage tools can either instrument the object code or source code. Java code coverage tools can be categorized into two sections: tools that instrument the source code and tools that instrument the bytecode.

Source code instrumentation is easier but requires source code recompilation. Bytecode instrumentation is complex but doesn't require source code recompilation.

The following are the available Java code coverage tools:

  • Cobertura: This tool instruments the bytecode offline and is a widely used coverage tool. Cobertura is an open source project (GNU GPL) and is very easy to configure with Eclipse and build tools. Version 1.9, which was released in March 2010, is the latest stable version.

  • EMMA: This tool instruments the bytecode offline or on the fly and is distributed under the Common Public License (CPL). Version 2.1, released in June 2005, is the latest version. Google CodePro AnalytiX is based on EMMA.

  • Clover: This tool instruments...

Measuring coverage using Gradle


Gradle can be configured to generate coverage reports using JaCoCo. This section will explain how to configure the Gradle JaCoCo plugin in your project.

The following are the steps to configure the Gradle plugin:

  1. Create a base folder named Chapter05 under any directory, such as D:/Packt; then, add a lib folder under Chapter05 and copy the JUnit4 and hamcrest JARs to the lib folder. Add another folder named Chapter05 under the base folder Chapter05 for the Java project. As per Gradle conventions, source files are kept under src/main/java and test files are kept under src/test/java. Create the directories under Chapter05\Chapter05.

    Tip

    This Chapter05 naming strategy is used for you to easily track the project and download the code from the Packt Publishing website, but your code should express the intent of the code. The name Chapter05 doesn't make any sense, maybe you can name it something like SimpleGradleProject or GradleCoverageProject.

  2. Copy the content of the...

Working with the Maven Cobertura plugin


Maven has a Cobertura plugin to measure code coverage; this section will explain how to configure the Cobertura Maven plugin in your project.

Tip

Cobertura uses asm to instrument the bytecode. The asm framework is a Java bytecode manipulation and analysis framework. Visit http://asm.ow2.org/ for asm details. Cobertura modifies the .class file, imports net.sourceforge.cobertura.coveragedata.*, implements the HasBeenInstrumented interface, and adds code to capture coverage, such as ProjectData.getGlobalProjectData().getOrCreateClassData("com.packt.coverage.Metrics").touch(21);.

After instrumenting the bytecode, Cobertura creates a .ser file and updates the file during test execution. This .ser file contains the test coverage details. The instrumented bytecode can be slightly slower than normal without it.

Follow the ensuing steps to configure Maven to generate a Cobertura report:

  1. Create a pom.xml file and place it under /Chapter05/Chapter05.

  2. Modify the pom...

Running the Cobertura Ant task


This section will explain how to configure the Cobertura Ant task in your project.

The following are the steps for configuration:

  1. Gradle and Maven can download the coverage tool JARs while running the build, but Ant needs the Cobertura JAR files to the classpath. Download the Cobertura ZIP file from http://cobertura.github.io/cobertura/.

  2. Extract the ZIP file and copy all JAR files in the downloaded ZIP to Chapter05\lib. Include all JARs from the lib folder and cobertura.jar from the root folder.

  3. Create a build.properties file under Chapter05\Chapter05 and enter the following information:

    src.dir=src/main/java
    test.dir=src/test/java
    # The path to cobertura.jar
    cobertura.dir=../lib
    classes.dir=classes
    instrumented.dir=instrumented
    reports.dir=reports
    # Unit test reports from JUnit are deposited into this directory
    reports.xml.dir=${reports.dir}/junit-xml
    reports.html.dir=${reports.dir}/junit-html
    coverage.xml.dir=${reports.dir}/cobertura-xml
    coverage.summaryxml.dir...

Summary


In this chapter, code coverage is described in depth and examples are provided to measure code coverage using Eclipse plugins and various coverage tools, such as Clover, JaCoCo, EclEmma, and Cobertura. We have also configured Ant, Maven, and Gradle to generate code coverage reports using coverage tools.

By the end of this chapter, you should be able configure Eclipse plugins and build scripts to measure code coverage.

The next chapter covers the static code analysis, code metrics, and various open source tools. It configures and uses PMD, Checkstyle, and FindBugs to analyze code quality and explores the Sonar code quality dashboard.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Mastering Unit Testing Using Mockito and JUnit
Published in: Jul 2014Publisher: ISBN-13: 9781783982509
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Sujoy Acharya

Sujoy Acharya works as a Principal Engineer with Cerner. While growing up, he pursued his interests in the fields of computer science and engineering. His hobbies are watching movies and sitcoms, playing outdoor sports, and reading books. Sujoy likes to research upcoming technologies. His major contributions are in the fields of TDD, building scalable applications, cloud services, and the Spring Framework. He has authored four books for Packt, namely, Test-Driven Development with Mockito, Mastering Unit Testing using Mockito and JUnit, Mockito Essentials, and Mockito for Spring.
Read more about Sujoy Acharya