Merge mode enables Diffblue Cover to merge generated tests into existing test classes rather than creating separate files.
Overview
Merge mode is an optional test generation approach where Diffblue Cover merges generated tests into existing test classes rather than creating separate test files. This provides a more natural integration with your existing test suite.
Simpler Project Structure - All tests for a class live in one file, with no separate *DiffblueTest files
Easier Navigation - Find and maintain tests in a single location alongside your manual tests
Smarter Test Class Coverage Optimization - Diffblue Cover considers your manual tests, then provides only the generated tests that add unique coverage. See Test Coverage Optimizations for details.
Enabling Merge Mode
CLI
To use merge mode with the CLI, add the --merge flag:
With --merge, the default class name template changes to ${CLASS_NAME}Test. If your existing test classes use a different naming convention, you can override this with --class-name-template:
See Test naming for all available template variables.
IntelliJ Plugin
Merge mode is enabled by default in the IntelliJ plugin. However, the plugin uses ${CLASS_NAME}DiffblueTest by default, which keeps generated tests in separate files.
To take advantage of merge mode, update the class name template in your plugin settings to match your existing test naming conventions (e.g., ${CLASS_NAME}Test). This allows generated tests to merge into your existing test classes.
When using --merge, the cover-annotations library version 1.4.0 or later is required.
If the library is missing or outdated when using --merge, Diffblue Cover displays an error during environment checks and halts execution. This requirement does not apply when running without --merge.
Add to your pom.xml:
Add to your build.gradle:
Add to your build.gradle.kts:
See Cover Annotations for more information about the annotations library.
Troubleshooting
R090: Failed to Merge Tests
When you see this error:
What R090 means: Diffblue Cover attempted to merge generated tests into an existing test class, but the merge failed. This happens when:
The existing test class has code that conflicts with the generated tests
Tests fail validation after merging (compile errors, test failures)
Solution 1: Use dcover issues --prompt (Recommended)
Run dcover issues --prompt to get an AI-assistant-ready prompt that will help you apply the necessary annotations:
Solution 2: Use @WriteTestsTo Annotation
Add the @WriteTestsTo annotation to direct tests to a separate file:
[R090] Failed to merge tests into test class
Failed to merge tests into test class for com.example.MyService.
To resolve this, you can:
- Add @WriteTestsTo("MyServiceDiffblueTest") annotation to MyService, or
- Run Diffblue Cover without the --merge flag
You can customize the annotation value to any valid Java class name.
dcover issues --prompt
package com.example;
import com.diffblue.cover.annotations.WriteTestsTo;
@WriteTestsTo("MyServiceDiffblueTest")
public class MyService {
// Tests will be written to MyServiceDiffblueTest.java
// instead of merging into MyServiceTest.java
}