Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
forcedotcom avatar

Apex Test Class

  • 16 installs
  • 787 repo stars
  • Updated August 5, 2026
  • forcedotcom/afv-library

Generates Apex test classes with TestDataFactory patterns, bulk testing of 200+ records, callout mocking, and meaningful assertions.

About

Produces Apex test classes using @TestSetup, TestDataFactory, bulk data, HttpCalloutMock, and negative-path assertions. A developer uses it when creating or improving test coverage for Apex triggers, services, controllers, batch jobs, and integrations.

  • Bulkifies tests with 200+ records and isolated TestDataFactory data
  • Mocks callouts with HttpCalloutMock and tests negative paths

Apex Test Class by the numbers

  • 16 all-time installs (skills.sh)
  • Ranked #1,468 of 2,153 Testing & QA skills by installs in the Skillselion catalog
  • Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/forcedotcom/afv-library --skill apex-test-class

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs16
repo stars787
Last updatedAugust 5, 2026
Repositoryforcedotcom/afv-library

What it does

Generates Apex test classes with TestDataFactory patterns, bulk testing of 200+ records, callout mocking, and meaningful assertions.

Files

SKILL.mdMarkdownGitHub ↗

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

@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:

  • [references/test-data-factory.md](references/test-data-factory.md) - TestDataFactory class patterns and field defaults
  • [references/assertion-patterns.md](references/assertion-patterns.md) - Assertion best practices and common pitfalls
  • [references/mocking-patterns.md](references/mocking-patterns.md) - HttpCalloutMock, Test.setMock(), stubbing
  • [references/async-testing.md](references/async-testing.md) - Batch, Queueable, Future, Scheduled job testing

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

Related skills

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.