Java application with code coverage report using gradle

3 minute read ~

Although we can use IDE for creating new java projects, if we use a build tool like gradle and create the project from ground up then not only we have more control, we can also change between IDEs as we feel the need.

Create new application

Create new java-application type Java project using gradle -

gradle init --type java-application

Please note: The Build Init plugin is currently incubating. Please be aware that the DSL and other configuration may change in later Gradle versions. To know more about gradle supported java projects read Gradle Build Init plugin documentation.

This command create a gradle.build file with the contents

/*
 * This build file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * user guide available at https://docs.gradle.org/4.2.1/userguide/tutorial_java_projects.html
 */

// Apply the java plugin to add support for Java
apply plugin: 'java'

// Apply the application plugin to add support for building an application
apply plugin: 'application'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    compile 'com.google.guava:guava:22.0'

    // Use JUnit test framework
    testCompile 'junit:junit:4.12'
}

// Define the main class for the application
mainClassName = 'App'

Gradle tasks

To check all tasks of the project run -

gradle task --all

You will see list of tasks available for the project and short description about what the task is about. If you want to run any of the task then just run

gradle <task name>

where <task name> is the task from the previous command. For example If you run -

gradle build

then the distribution(compressed executable) file will be found in buld>distributions folder. There is tar and zip archive. You can extract and use either of them.

You can create new custom tasks or add plugin that have prebuilt task that you can use.

Code Coverage metric

Code coverage is one of the most important metric for any project. From Wikipedia -

In computer science, test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs. A program with high test coverage, measured as a percentage, has had more of its source code executed during testing which suggests it has a lower chance of containing undetected software bugs compared to a program with low test coverage.

We are going to use JaCoCo1 for test coverage. At first we need to add JaCoCo as dependencies -

...
// Apply the jacoco plugin to add support for test coverage
apply plugin: 'jacoco'
...

Now if we run -

gradle task --all

we will see that there is two new task available under Verification tasks

Verification tasks
------------------
check - Runs all checks.
jacocoTestCoverageVerification - Verifies code coverage metrics based on specified rules for the test task.
jacocoTestReport - Generates code coverage report for the test task.
test - Runs the unit tests.

now we can create code coverage report by running -

gradle jacocoTestReport

but I want to create code coverage report when I run gradle test command which run all tests for the project. To do so edit your gradle.build file -

...
// create test coverage every time the test task is performed
test.finalizedBy jacocoTestReport

Now run gradle build or gradle test to generate the Code Coverage Report. The Code Coverage Report can be found in build>reports>jacoco>test>html. Open the index.html file here to view the Coverage Report

Please Note: We can use JaCoCo to generate Code Coverage Report for Continuous Code Quality tools like SonarQube2. I will discuss about it in future articles.

References:

  1. JaCoCo is a free code coverage library for Java, which has been created by the EclEmma team based on the lessons learned from using and integration existing libraries for many years. 

  2. SonarQube - SonarQube is an open source platform for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells and security vulnerabilities on 20+ Programming languages. SonarQube offers reports on duplicated code, coding standards, unit tests, code coverage, code complexity, comments, bugs, and security vulnerabilities. SonarQube can record metrics history and provides evolution graphs. SonarQube’s greatest asset is that it provides fully automated analysis and integration with Maven, Ant, Gradle, MSBuild and continuous integration tools. SonarQube also integrates with Eclipse, Visual Studio and IntelliJ IDEA development environments through the SonarLint plugins and integrates with external tools like LDAP, Active Directory, GitHub, etc. 

Leave a Comment