AgentSkillsCN

apex-test-class

运用TestDataFactory模式生成Apex测试类,支持批量测试(200条以上记录)、针对调用外部服务与异步操作的模拟策略,以及断言的最佳实践。适用于新建Apex测试类、提升测试覆盖率、重构现有测试,或为触发器、服务、控制器、批处理作业、Queueable以及集成场景实施恰当的测试模式时使用。

SKILL.md
--- frontmatter
name: apex-test-class
description: Apex test class generation with TestDataFactory patterns, bulk testing (200+ records), mocking strategies for callouts and async operations, and assertion best practices. Use when creating new Apex test classes, improving test coverage, refactoring existing tests, or implementing proper testing patterns for triggers, services, controllers, batch jobs, queueables, and integrations.

Apex Test Class Skill

Core Principles

  1. Bulkify tests - Always test with 200+ records to catch governor limit issues
  2. Isolate test data - Use @TestSetup and TestDataFactory; never rely on org data
  3. Assert meaningfully - Test behavior, not just coverage; include failure messages
  4. Mock external dependencies - Use HttpCalloutMock, Test.setMock() for integrations
  5. Test negative paths - Validate error handling, not just happy paths

Test Class Structure

apex
@IsTest
private class MyServiceTest {

    @TestSetup
    static void setupTestData() {
        // Create shared test data using TestDataFactory
        List<Account> accounts = TestDataFactory.createAccounts(200, true);
    }

    @IsTest
    static void shouldPerformExpectedBehavior_WhenValidInput() {
        // Given: Setup specific test state
        List<Account> accounts = [SELECT Id, Name FROM Account];
        
        // When: Execute the code under test
        Test.startTest();
        MyService.processAccounts(accounts);
        Test.stopTest();
        
        // Then: Assert expected outcomes
        List<Account> updated = [SELECT Id, Status__c FROM Account];
        System.Assert.areEqual(200, updated.size(), 'All accounts should be processed');
        for (Account acc : updated) {
            System.Assert.areEqual('Processed', acc.Status__c, 'Status should be updated');
        }
    }

    @IsTest
    static void shouldThrowException_WhenInvalidInput() {
        // Given
        List<Account> emptyList = new List<Account>();
        
        // When/Then
        Test.startTest();
        try {
            MyService.processAccounts(emptyList);
            System.Assert.fail('Expected MyCustomException to be thrown');
        } catch (MyCustomException e) {
            System.Assert.isTrue(e.getMessage().contains('cannot be empty'), 
                'Exception message should indicate empty input');
        }
        Test.stopTest();
    }
}

Naming Convention

Use descriptive method names: should[ExpectedBehavior]_When[Condition]

Examples:

  • shouldCreateContact_WhenAccountIsActive
  • shouldThrowException_WhenEmailIsInvalid
  • shouldSendNotification_WhenOpportunityClosedWon
  • shouldBypassTrigger_WhenRunningAsBatch

Test.startTest() / Test.stopTest()

Always wrap the code under test:

  • Resets governor limits for accurate limit testing
  • Executes async operations synchronously (queueables, batch, future)
  • Fires scheduled jobs immediately

Reference Files

Detailed patterns for specific scenarios:

Quick Reference: What to Test

ComponentKey Test Scenarios
TriggerBulk insert/update/delete, recursion, field changes
ServiceValid/invalid inputs, bulk operations, exceptions
ControllerPage load, action methods, view state
BatchStart/execute/finish, chunking, error records
QueueableChaining, bulkification, error handling
CalloutSuccess response, error response, timeout
ScheduledExecution, CRON validation