Knowledge Base > Diffblue Cover in the CI Pipeline > Quick Start Guide for Jenkins

Quick Start Guide for Jenkins


This guide explains in few simple steps how to use Diffblue Cover to write tests in Jenkins CI. This guide assumes that you have:

  • A Maven or Gradle project that:

    • Compiles
    • Does not have non-compiling or failing tests
    • Triggers Jenkins CI on push or pull request with a declarative Jenkinsfile
  • A basic understanding of Jenkins

  • The ability to add credentials to, and generally manage, the Jenkins server

  • Diffblue Cover stored in the cloud for download along with the license key (or pre-installed and activated in your CI machines). See Installation.

To integrate Diffblue Cover into your CI pipeline, we will guide you through creating a Jenkinsfile that:

  1. Builds your project

  2. Downloads and activates Diffblue Cover CLI

  3. Runs Diffblue Cover to create tests

  4. Commits the created tests to a branch

The following sections provide more details for each of the above steps. After completing this guide, you can continue on to further examples of adding Diffblue Cover to your CI pipeline in an automated way.

1. Building the project

In your Jenkinsfile, add the Jenkins stage Use dcover create in Jenkins to the Jenkinsfile. Make sure to check, and if necessary modify, the build command to build your project. Running the project’s tests is not required and you will save time by skipping them, but they do need to compile and pass.

pipeline {
    agent any

    stages {
        stage('Use dcover create in Jenkins') {
            steps {
                sh '''
                    echo "Build project, you may need to update this to reflect your project"
                    mvn clean install -DskipTests


Push this change so that CI is triggered. When this runs on Jenkins, you should see in the console log that the project has successfully built. If this is not working, this is out of the scope of this tutorial. See the Jenkins documentation, or you may need to double-check your project configuration.

2. Downloading and activating Diffblue Cover CLI

You need to give the CI run access to the Diffblue Cover files and activate the dcover license in order to write tests.

This guide assumes that you have a URL with the Diffblue Cover CLI release zip and the license key for online activation during the CI run. Alternatively, it can be pre-installed on the machines running CI with online activation. See the separate page on Installation. If the machines running CI are not able to access the internet for activation, pre-installing Diffblue Cover with offline activation may be possible if your license allows it. See the Diffblue Cover licensing documentation.

Add secret text credentials with id diffblue-cover-release-url and set the value to the URL of the Diffblue Cover CLI release zip file. Then add secret text credentials with id diffblue-cover-license-key and set the value to your Diffblue Cover license key.

Then add this environment section to your Jenkinsfile.

environment {
    DB_RELEASE_URL = credentials('diffblue-cover-release-url')
    DB_LICENSE_KEY = credentials('diffblue-cover-license-key')

Append the code for getting, unzipping and activating dcover to the shell script in the Jenkinsfile.

stages {
    stage('Use dcover create in Jenkins') {
        steps {
            sh '''


                echo "Get and unzip dcover jars into directory dcover, store dcover script location for later use"
                mkdir --parents dcover
                wget "$DB_RELEASE_URL" --output-document dcover/ --quiet
                unzip -o dcover/ -d dcover

                echo "Activate dcover"
                "$DCOVER_SCRIPT_LOCATION" activate "$DB_LICENSE_KEY"


This will put the Diffblue Cover files into the directory dcover in the root of the workspace. The Diffblue Cover files contain a script to invoke dcover which has the relative path dcover/dcover. This is stored in the variable DCOVER_SCRIPT_LOCATION.

Push the changes so that CI runs and ensure that you can see the successful activation of dcover before moving on. If this is not working, please see the Diffblue Cover licensing documentation or contact Diffblue Support.

3. Running Diffblue Cover CLI to create tests

Now that Diffblue Cover is installed in CI, you can use it to write tests. The next two sections show how to write tests for a single module, and then how to extend this to all modules.

Creating tests for a single module

Choose a module to test in your project. In the script started above in the Jenkinsfile, append the following - changing moduleToTest to a module in your project or, if your project does not have modules, --working-directory moduleToTest can be removed or changed to --working-directory .. The option --batch makes the output more suitable for CI, as it ensures the CI logs are not cluttered with progress bar output.

stages {
    stage('Use dcover create in Jenkins') {
        steps {
            sh '''

                echo "Running dcover with create on create, you may need to update this to reflect your project"
                "$DCOVER_SCRIPT_LOCATION" create --working-directory "moduleToTest" --batch


Push the changes so that CI runs. Once successfully complete, you should expect to see output that looks like this:

INFO  Found 7 callable methods in 2 classes
INFO  Creating tests:
INFO  ---------------
INFO  All 5 created tests were successfully validated.

If you do not see this, the call may need tweaking for your project or dependencies adding until it works. The output gives you warnings along the way to guide you. See the Diffblue Cover CLI documentation.

Depending on the size of your module/project, this could take a while. You may wish to restrict test creation to a single class by specifying its fully qualified name:

"$DCOVER_SCRIPT_LOCATION" create com.somepackage.SomeClass --working-directory "moduleToTest" --batch

Creating tests for all modules

To write tests for all the modules, add calls to dcover for each module. For example:

"$DCOVER_SCRIPT_LOCATION" create --working-directory "moduleToTest" --batch
"$DCOVER_SCRIPT_LOCATION" create --working-directory "anotherModuleToTest" --batch
"$DCOVER_SCRIPT_LOCATION" create --working-directory "yetAnotherModuleToTest" --batch

4. Committing the created tests to a branch

To see the new tests created in the previous step in your project you need to commit them and push back to the repository. Assuming your Jenkins sever is already configured to allow you to push changes, you will need to configure your git credentials. We recommend creating a service account for this.

  git config db-ci-bot
  git config [email protected]

To commit the tests append the following to your script. This will check for any changes to Diffblue tests, add them to a commit and push to your branch.

  if [ -n "$(git status --short **/*" ]; then 
    git add **/*
    git commit --message "Update Unit Tests for $(git rev-parse --short HEAD)"
    git push --set-upstream origin
    echo "Nothing to commit"

What’s next

It is possible to integrate Diffblue Cover completely automatically into your CI workflow. Listed here are ideas and examples, each building on another, for what to try next:

1) Export a patch file with the new tests as an artifact for the user to use locally

2) Set up a job that a user can trigger that pushes the Diffblue tests back to the change branch as a Jenkins freestyle project.

3) Trigger the above job using a webhook for every pull request in a Jenkins multi-branch declarative pipeline