Repository navigation
Description
Proposed Rule Name: ApexUnitTestClassShouldHaveRunRelevantTestsAnnotation
Proposed Category: Best Practices
Description:
Salesforce introduced two new (Beta, API v66.0+) modifiers for the @IsTest annotation, used together with the RunRelevantTests deployment test level:
@IsTest(critical=true)— guarantees the test class always runs during aRunRelevantTestsdeployment, regardless of the payload.@IsTest(testFor='ApexClass:Name, ApexTrigger:Name, ...')— guarantees the test class runs whenever the deployment includes new/modified versions of the referenced Apex classes/triggers.
Without one of these modifiers, a test class relies entirely on Salesforce's automatic dependency detection to decide whether it runs during a RunRelevantTests deployment. For test classes covering critical paths, or classes that exercise code reached only through dynamic mechanisms (e.g. Type.forName, metadata-driven routing/dispatch), automatic detection can miss the dependency, and the test class may silently be skipped during a deployment even though the code it protects changed.
This rule follows the same "should have" pattern as ApexUnitTestClassShouldHaveRunAs and ApexUnitTestClassShouldHaveAsserts, and flags test classes that use neither critical nor testFor on their @IsTest annotation, prompting the author to consciously decide whether the class needs one of these guarantees.
Important: since critical and testFor are only available starting with API version 66.0, classes compiled/deployed with a lower API version don't have access to these modifiers at all and should not be flagged by this rule. The rule should only consider classes whose API version (metadata apiVersion, as already surfaced on the Apex AST for other version-gated rules) is >= 66.0.
Code Sample: This should include code, that should be flagged by the rule. If possible, the "correct" code according to this new rule should also be demonstrated.
// Flagged (API version >= 66.0): no 'critical' or 'testFor' modifier
@IsTest
private class OpportunityEODTest {
@IsTest
static void validatesOrgWideBehavior() {
// ...
}
}
// Not flagged (API version < 66.0): modifiers aren't available yet
@IsTest
private class LegacyTest {
@IsTest
static void testSomething() {
// ...
}
}
// Good: explicitly marked as always relevant
@IsTest(critical=true)
private class OpportunityEODTest {
@IsTest
static void validatesOrgWideBehavior() {
// ...
}
}
// Good: explicitly tied to its dependencies
@IsTest(testFor='ApexClass:InvoiceEngine, ApexTrigger:InvoiceTrigger')
private class InvoiceDynamicFlowTests {
@IsTest
static void validatesDynamicInvocationPath() {
// ...
}
}
Activity
[-][apex] New rule: detect usage of @IsTest(critical=true) / @IsTest(testFor='...') annotations (RunRelevantTests, Beta, API 66.0+)[/-][+][apex] New rule: Detect usage of @IsTest(critical=true) / @IsTest(testFor='...') annotations (RunRelevantTests, Beta, API 66.0+)[/+][doc] Release notes (pmd#6988, pmd#6991)
[apex] Fix pmd#6988: Add ApexUnitTestClassShouldHaveRunRelevantTestsA…