LogoLogo
diffblue.comFree trial
  • Discover Diffblue Cover
  • Get Started
    • What is Diffblue Cover?
    • Get started
      • Free trial
      • Get started - Cover Plugin
      • Get started - Cover CLI
      • Get started - Cover Pipeline
      • Get started - Cover Reports
    • Specs & Reqs
    • Reference Deployments
    • Licensing
      • Online license activation
      • Offline license activation
      • Diffblue License Manager
    • Update Cover
    • FAQs
    • Diffblue Learning
      • Get started
        • Just the basics
        • Free trial
        • Cover Plugin (IDE)
        • Cover CLI (Command Line)
      • Developer
        • Unit tests (IDE)
        • Unit tests (CLI)
        • Test coverage
      • DevOps
        • GitHub
        • GitLab
        • Other CI
      • Administrator
        • Admin - IntelliJ
        • Admin - CLI
        • Admin - Reports
      • Test coverage
        • Developer
        • Senior developer
        • Cover Reports Administrator
  • EVALUATION & ONBOARDING
    • Proof of Value
    • Jumpstart
      • Prerequisites for onboarding
      • Phase 1: Up and running
        • Module 1: Create your Cover unit test baseline
        • Module 2: Cover Pipeline CI integration
      • Phase 2: Developer productivity
        • Module 3: Getting started using Cover
        • Module 4: Introduction to Cover Reports
      • Phase 3: Advanced topics
        • Module 5: Speed up your test execution
        • Module 6: Getting more from Cover
  • Features
    • Cover Plugin
      • Writing tests
        • Gutter icons
        • Menu options
        • Run configurations
        • Cover Plugin tool window
        • Test Review
        • Test examples
        • Creating partial tests
        • Creating skeleton tests
        • Covering all enum values
        • Test insertion order
        • Diffblue Sandbox
        • Environment Check Cache
      • Project configuration
        • General dependencies
        • Test framework dependencies
      • Cover Plugin settings
        • Test Naming
        • Test Formatting
        • Spring configuration options
        • Method Annotations
        • Test Directory
        • Reset Cover Plugin settings
      • Cover Plugin admin
        • Core Maintenance
        • Cover Plugin toolbar menu
        • Cover Plugin status bar widget
        • Telemetry
        • Memory management
        • Using SonarQube with Cover Plugin
        • Log files
        • Troubleshooting
    • Cover CLI
      • Writing tests
        • Command summary
        • Test examples
        • Creating partial tests
        • Customizing test inputs
        • Customizing test setup
        • Test naming
        • Test formatting
        • Test insertion order
        • Patch files
        • Diffblue Sandbox
        • Operational behaviors
        • Test validation
      • Project configuration
        • Preflight checks
        • General dependencies
        • Test framework dependencies
        • Compiling your project successfully
          • Building a Maven project
          • Building a Gradle project
        • Configuring Cover to work with your project's build system
          • Quick guide
          • Configuration file in detail
          • Getting Started with Ant Projects
          • Default configuration
        • Mocking using Mockito
        • Spring profiles
        • Runtime environment
      • Commands & Arguments
        • Environment configuration for CI
        • Packages, classes, and methods
        • Quoting command line arguments
        • Argument files
      • Cover CLI admin
        • Core Maintenance
        • Telemetry
        • Memory management
        • Using Cover CLI in Eclipse
        • Using SonarQube with Cover CLI
        • Log files
        • Troubleshooting
      • Environment Configuration
    • Cover Pipeline
      • Cover Pipeline for CI
        • Quick Start - General
        • Quick Start - Jenkins
        • Quick Start - Azure Pipelines
        • Quick Start - AWS Codebuild
        • Integrating Diffblue Cover into CI on pull requests
        • Installation - VMs or CI run
      • Cover Pipeline for GitLab
        • Introduction
        • Installation and initial setup
        • GitLab workflow
        • Configuration
        • Troubleshooting
        • Related topics
      • Cover Pipeline for GitHub
        • Introduction
        • Installation and initial setup
        • GitHub workflow
        • Configuration
        • Troubleshooting
        • Related topics
    • Cover Reports
      • Cover Reports Contributor
        • Java project config (JaCoCo)
        • Generate and upload reports bundles
        • Authenticated uploads
      • Cover Reports User
        • Navigation
        • Dashboards
        • Telemetry data
        • Export API
        • Considerations
      • Cover Reports Administrator
        • Install and update Cover Reports
        • Configuration options
        • Database backup
        • SSO with Cover Reports
        • Uninstall Cover Reports
    • Cover Optimize
      • Get started - Cover Optimize
      • Cover Optimize & Gradle
      • Cover Optimize & Maven
      • Patch files
    • Cover Refactor
      • Get started - Cover Refactor
    • Cover Annotations
      • Mocking Annotations
      • Custom Input Annotations
      • Interesting Value Annotations
    • Output Codes
      • E - Environment Codes
      • L - License Codes
      • R - Reason Codes
      • T - Testability Codes
      • V - Validation Codes
      • Working with output codes
        • Working with code E020
        • Working with codes E057 to E065
        • Working with code E085
        • Working with code R005
        • Working with code R006
        • Working with code R008
        • Working with code R011
        • Working with code R012
        • Working with code R013
        • Working with code R026
        • Working with code R031
        • Working with code V003
    • Tutorials
      • How to measure test quality
      • How to improve code coverage
      • How to test a new feature
      • How to find regressions
      • How to use Diffblue Cover in test driven development (TDD)
      • How to write tests for Kotlin projects
      • Examples of tests created by Diffblue Cover
      • Best practices for testing private methods
  • Updates & Upgrades
    • Update Cover
    • Cover Editions
    • What's new
    • Release archive
      • 2025-05-01
      • 2025-04-02
      • 2025-04-01
      • 2025-03-02
      • 2025-03-01
      • 2025-02-02
      • 2025-02-01
      • 2025-01-02
      • 2025-01-01
      • 2024-12-02
      • 2024-12-01
      • 2024-11-02
      • 2024-11-01
      • 2024-10-02
      • 2024-10-01
      • 2024-09-02
      • 2024-09-01
      • 2024-08-02
      • 2024-08-01
      • 2024-07-04
      • 2024-07-03
      • 2024-07-01
      • 2024-06-02
      • 2024-06-01
      • 2024-05-02
      • 2024-05-01
      • 2024-04-02
      • 2024-04-01
      • 2024-03-02
      • 2024-03-01
      • 2024-02-02
      • 2024-02-01
      • 2024-01-02
      • 2024-01-01
      • 2023-12-02
      • 2023-12-01
      • 2023-11-03
      • 2023-11-02
      • 2023-11-01
      • 2023-10-02
      • 2023-10-01
      • 2023-09-02
      • 2023-09-01
      • 2023-08-02
      • 2023-08-01
      • 2023-07-03
      • 2023-07-02
      • 2023-07-01
      • 2023-06-02
      • 2023-06-01
      • 2023-05-02
      • 2023-05-01
      • 2023-04-02
      • 2023-04-01
      • 2023-03-02
      • 2023-03-01
      • 2023-02-02
      • 2023-02-01
      • 2023-01-02
      • 2023-01-01
      • 2022-12-02
      • 2022-12-01
      • 2022-11-02
      • 2022-11-01
      • 2022-10-03
      • 2022-10-02
      • 2022-10-01
      • 2022-09-02
      • 2022-09-01
      • 2022-08-05
      • 2022-08-03
      • 2022-08-02
      • 2022-08-01
      • 2022-07-02
      • 2022-07-01
      • 2022-06-02
      • 2022-06-01
      • 2022-05-02
      • 2022-05-01
      • 2022-04-02
      • 2022-04-01
      • 2022-03-02
      • 2022-03-01
      • 2022-02-02
      • 2022-02-01
      • 2022-01-02
      • 2022-01-01
      • 2021-06-02
      • 2021-02-01
  • Legal
    • Diffblue Legal
      • Diffblue End User License Agreement (EULA)
      • Cover Third Party Notices and/or Licenses
      • Privacy Notice
Powered by GitBook
On this page
  • Overview
  • Configuring build system: Maven
  • Install the Cover Optimize for Maven plugin
  • Configure the Cover Optimize for Maven plugin to run on your project
  • Running for the first time: Cover Optimize
  • Create a patch file
  • Execute tests with Diffblue Cover Optimize
  • Adding Diffblue Cover Optimize into your CI system
  • Installing Diffblue Cover in your CI environment
  • Example
  • Defining custom rules

Was this helpful?

Export as PDF
  1. Features
  2. Cover Optimize

Cover Optimize & Maven

PreviousCover Optimize & GradleNextPatch files

Last updated 1 year ago

Was this helpful?

Cover Optimize speeds up the time required to run Java unit tests by running only the tests in your project that are impacted by your code change. Our Cover Optimize for Maven plugin accepts a patch file, analyses the code change and determines which Java unit tests in your project need to be run in order to exercise the changes in the patch.

To use the Cover Optimize for Maven plugin, Cover CLI (release >= 2022.03.02) must be installed and activated with an appropriate license. Further installation details can be found .

Overview

Cover Optimize can be integrated into your CI with the help of the Cover Optimize for Maven plugin. This plugin acts as a wrapper around the Cover CLI, invoking Cover Optimize during mvn test or mvn verify, and feeding its output into Maven's Surefire & Failsafe plugins in order to only run the tests exercised by the patch.

The below graphic illustrates how Diffblue Cover Optimize is invoked from cover-maven-plugin within a Maven project:

Configuring build system: Maven

Install the Cover Optimize for Maven plugin

Add the Diffblue Public Maven Repository to your project's root POM file in order to download the plugin:

<pluginRepositories>
    <pluginRepository>
       <id>maven-diffblue-repository</id>
       <name>Diffblue Public Maven Repository</name>
       <url>https://maven.diffblue.com/release</url>
       <snapshots>
            <enabled>false</enabled>
       </snapshots>
    </pluginRepository>
</pluginRepositories>

If you already have a <pluginRepositories> section in the POM file, add the Diffblue Repository at the bottom of the section.

Configure the Cover Optimize for Maven plugin to run on your project

To activate the plugin, please add the skipTestOptimizer and skipTests properties in the <properties> section of your project's POM file:

<properties>
    <skipTestOptimizer>false</skipTestOptimizer>
    <skipTests>false</skipTests>
</properties>

If either of these properties are set to true, then Cover Optimize will be skipped.

<properties>
    ...
    <com.diffblue.cover.unitTestPattern>**/UnitTest*.java, **/*UnitTest.java</com.diffblue.cover.unitTestPattern>
    <com.diffblue.cover.integrationTestPattern>**/*IntTest*.java, **/*ITest.java</com.diffblue.cover.integrationTestPattern>
</properties>

These may also be specified directly in the call to mvn test

mvn test -Dcom.diffblue.cover.unitTestPattern="**/UnitTest*.java, **/*UnitTest.java" -Dcom.diffblue.cover.integrationTestPattern="**/*IntTest*.java, **/*ITest.java"

Next, add the Cover Optimize for Maven plugin to the <build><plugins> (or <build><pluginManagement><plugins> depending on how your project is configured) section of your project's POM file:

<build>
    <plugins>
        <plugin>
            <groupId>com.diffblue.cover</groupId>
            <artifactId>cover-maven-plugin</artifactId>
            <version>[diffblue-cover-version]</version>
            <executions>
                <execution>
                <phase>process-test-classes</phase>
                <goals>
                    <goal>optimize</goal>
                </goals>
                </execution>
            </executions>
            <configuration>
                <skip>${skipTestOptimizer}</skip>
                <skipTests>${skipTests}</skipTests>
                <failOnError>false</failOnError>
            </configuration>
        </plugin>
    </plugins>
</build>

In the above example, you should replace [diffblue-cover-version] with the version number of Diffblue Cover you are using e.g. <version>2022.03.02</version> for version 2022.03.02.

The plugin needs to have both the source and test class files compiled in order to perform its analysis, hence the use of the process-test-classes phase. The plugin then provides input to the subsequent phases for executing the tests.

In order to run Diffblue Cover Optimize against any unit tests, add a <failIfNoTests> and an <include> entry into the <configuration> section of maven-surefire-plugin (the full plugin declaration is displayed here for reference, if you already have the plugin configured, simply add the above two elements into your existing configuration):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>3.0.0-M5</version>
    <configuration>
        <failIfNoTests>false</failIfNoTests>
        <includes>
            <include>${com.diffblue.selectedTests}</include>
        </includes>
    </configuration>
</plugin>

In order to run Diffblue Cover Optimize against any integration tests, add a <failIfNoTests> and an <include> entry into the <configuration> section of maven-failsafe-plugin (the full plugin declaration is displayed here for reference, if you already have the plugin configured, simply add the above two elements into your existing configuration):

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>3.0.0-M5</version>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    <configuration>
        <failIfNoTests>false</failIfNoTests>
        <includes>
            <include>${com.diffblue.selectedITs}</include>
        </includes>
    </configuration>
</plugin>

The com.diffblue.selectedTests and com.diffblue.selectedITs properties are dynamically set by the Diffblue Cover Maven plugin. Due to the way Maven initializes properties, these properties must not be defined anywhere else in your POM files.

The use of <failIfNoTests>false</failIfNoTests> prevents Surefire and/or Failsafe from failing when no tests have been selected for a module.

Running for the first time: Cover Optimize

Create a patch file

Create a patch file containing the changes you wish to run Cover Optimize against. For examples on how to create a patch file from your changes using git, see Patch files.

Execute tests with Diffblue Cover Optimize

Then run mvn test (for unit tests) or mvn verify (for integration tests) appending the following two arguments:

  • -Dcom.diffblue.cover.command=/path/to/dcover, where /path/to/dcover is the absolute (not relative) path to Diffblue Cover CLI. Using a relative path such as ~/path/to/dcover will not work, the path must be absolute.

  • -Dcom.diffblue.cover.patch=/path/to/changes.patch, where /path/to/changes.patch is the absolute (not relative) path to the patch file generated above. Using a relative path such as ~/path/to/a/changes.patch will not work, the path must be absolute. e.g. mvn test -Dcom.diffblue.cover.command=/path/to/dcover -Dcom.diffblue.cover.patch=/path/to/changes.patch

Alternatively, instead of using Maven properties, you can also define these two values in environment variables (again, both paths must be absolute):

  • e.g. in bash run export DIFFBLUE_COMMAND=/path/to/dcover or in powershell run $env:DIFFBLUE_COMMAND=/path/to/dcover

  • e.g. in bash run export DIFFBLUE_PATCH=/path/to/a/changes.patch or in powershell run $env:DIFFBLUE_PATCH=/path/to/a/changes.patch and then you can simply run mvn test/mvn verify on its own.

You can check the value of all the parameters used by the plugin by running Maven in debug mode, e.g. using -X.

Finally, to run Cover Optimize, run mvn test (for unit tests) or mvn verify (for unit & integration tests) as usual. Only the tests that are impacted by the changes in the .patch file will be run.

Adding Diffblue Cover Optimize into your CI system

When Diffblue Cover Optimize is set up for your project, it can be used in place of your test command to save time running tests in a CI environment.

Installing Diffblue Cover in your CI environment

Example

Defining custom rules

There might be cases where Diffblue Cover Optimize does not select tests on certain changes as expected. It is easily possible to add custom rules to the configuration of the cover-maven-plugin to instruct Cover Optimize in these situations. For example:

<configuration>
  ...
  <rules>
    <rule>
      <filesChanged>**/pom.xml</filesChanged>
      <testsToRun>**/*Test.java,**/*IT.java</testsToRun>
    </rule>
    <rule>
      <filesChanged>**/resources/**,**/projects/**</filesChanged>
      <testsToRun>**/*IT.java</testsToRun>
    </rule>
  <rules>
</configuration>

The rules above mean that:

  • If there was a change to a pom.xml file then all tests with the suffixes Test and IT will be run.

  • If there was a change to any file inside a resources or projects directory then all tests with suffixes IT will be run. The matcher syntax uses glob patterns as used by the Surefire plugin to specify includes, for instance.

Custom rules are available from release 2022.05.02.

By default, Diffblue Cover Optimizer filters tests using the same default patterns as and If your test naming scheme is different, you can specify these patterns as a comma separated list of standard globs as properties. For example,

Before you begin the installation, please obtain the link to download Cover (from your product update email, or contact ). Please note you need version 2022.03.02 or above, installed and activated with an appropriate license.

For full details, please see: .

An example using Jenkins and Maven is available .

Surefire
Failsafe
Diffblue
Installing Diffblue Cover in your CI environment
here
here