Test organization annotations allow users to control how tests are organized and structured.
Using @WriteTestsTo
The @WriteTestsTo annotation is available from cover-annotations version 1.9.0.
The @WriteTestsTo annotation directs Diffblue Cover to write tests for a specific source class into a designated test class file, rather than following the default naming template (configured via --class-name-template).
Basic Usage
The specified test class name must be a valid Java classname, and the test class will be created (if it does not already exist) in the test folder under the same package structure as the source class.
packagecom.example.myapp;importcom.diffblue.cover.annotations.WriteTestsTo;@WriteTestsTo("CustomTestClassName")publicclassSourceClass{publicStringgetValue(){return"example";}}// Tests will be written to: src/test/java/com/example/myapp/CustomTestClassName.java
If an invalid classname is provided, Diffblue Cover will throw an error at environment checks stage.
This annotation can only be applied at the class level.
Using @WriteTestsTo as an Escape Hatch for Merge Failures
When using merge mode (--merge), Diffblue Cover attempts to merge generated tests into existing test classes. If the merge fails (indicated by output code R090), you can use @WriteTestsTo to direct tests for that specific class to a separate file.
Example - Resolving R090:
Use dcover issues --prompt to get AI-assistant-ready suggestions for which classes need @WriteTestsTo annotations.
See Merge Mode for full documentation on merge mode and troubleshooting.
package com.example.myapp;
import com.diffblue.cover.annotations.WriteTestsTo;
// This class's existing test file has conflicts with merge mode
@WriteTestsTo("MyServiceDiffblueTest")
public class MyService {
// Tests will be written to MyServiceDiffblueTest.java instead of MyServiceTest.java
}