Test Organization Annotations

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.

package com.example.myapp;

import com.diffblue.cover.annotations.WriteTestsTo;

@WriteTestsTo("CustomTestClassName")
public class SourceClass {
    public String getValue() {
        return "example";
    }
}
// Tests will be written to: src/test/java/com/example/myapp/CustomTestClassName.java
circle-exclamation
circle-info

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:

circle-info

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.

Last updated

Was this helpful?