Test method declared outside a test class
ID |
apex.correctness.test_in_test_class |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Test Quality |
Language |
Apex |
Description
Reports a method annotated @IsTest declared in a class that is not itself annotated @IsTest.
Rationale
Only a class annotated @IsTest is exempt from the org’s Apex character limit. A test method sitting in a business class carries its whole body, its test data and its assertions into that limit, and the org hits the ceiling sooner for code that never runs in production. The method also ships to production as part of the class rather than as a test artefact, so the test helpers, the sample records and any @TestVisible scaffolding it pulls in become part of the deployed surface.
The following code illustrates the pattern detected by this rule:
public class AccountService {
public static void touch(Account a) {
a.Description = 'touched';
update a;
}
// FLAGGED: Test method declared outside a test class
@IsTest
static void touchSetsDescription() {
Account a = new Account(Name = 'Acme');
AccountService.touch(a);
Assert.areEqual('touched', a.Description, 'the description was expected to change');
}
// FLAGGED: Test method declared outside a test class
@isTest
static void touchIsIdempotent() {
Account a = new Account(Name = 'Acme', Description = 'touched');
AccountService.touch(a);
Assert.areEqual('touched', a.Description, 'a second call changes nothing');
}
@TestVisible
private static String stamp() {
return 'touched';
}
}