Skip to content

[Feature Request] Enforce @IsTest(testFor) annotation on Apex test classes #2008

@bhavish030

Description

@bhavish030

Product Area

A Rule

Your Need or Problem

When working Apex test classes, there is no automated way to enforce that developers specify the testFor property on the @IsTest annotation (e.g., @IsTest(testFor='ApexClass:MyService')).

The testFor parameter was introduced to explicitly link a test class to the class it covers, improving traceability, code coverage mapping, and maintainability.

Your Desired Solution

Add a new PMD rule under the bestpractices category that:

Triggers when an Apex class is annotated with @IsTest (or @isTest) at the class level but does not include the testFor property.
Passes when @IsTest(testFor='ApexClass:ClassName') (or any valid testFor value) is present.
Has a configurable severity (defaulting to Warning/Moderate).

Alternatives Considered

No response

Additional Context (Screenshots, Files, etc)

Example of violation:

@IsTest          // ← missing testFor
public class MyServiceTest { ... }

Example passing:

@IsTest(testFor='ApexClass:MyService')
public class MyServiceTest { ... }

### Workaround

_No response_

### Urgency

Low

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions