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';
    }
}

Remediation

Move the method into a dedicated class annotated @IsTest, named after the class under test (AccountServiceTest). Keep the production class free of test code; where the test needs access to private state, mark that member @TestVisible instead of moving the test next to it.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved