Skip to main content

Code reviews rule: Missing annotation @IsTest in test methods

Written by David Martin
Updated this week

Missing annotation @IsTest in test methods

Why is this an issue?

The testMethod keyword for declaring test methods has been deprecated by Salesforce. Using the @IsTest annotation is the recommended and widely adopted approach.

While both approaches currently work, using deprecated syntax:

  • May cause issues in future Salesforce releases

  • Reduces code consistency when mixed with modern patterns

  • Does not support additional annotation parameters like @IsTest(SeeAllData=false)

Examples

Example of incorrect code:

private class AccountServiceTest {
static testMethod void testGetAccounts() {
// Test logic...
}
}

Example of correct code:

@IsTest
private class AccountServiceTest {
@IsTest
static void testGetAccounts() {
// Test logic...
}
}

How can I fix violations?

This rule supports autofix.

To manually fix violations:

  1. Replace the testMethod keyword with the @IsTest annotation on the method.

  2. Add the @IsTest annotation to the test class if it is not already present.

Resources

Did this answer your question?