agentleFS
Clientell-AiClientell-Ai/salesforce-skills/skills/sf-test/SKILL.md

sf-test

Skill17 starsChanged 9 months agoApache-2.0View on GitHub

Generate comprehensive Apex test classes with @TestSetup methods, TestFactory patterns, bulk data (200 records), positive/negative/permission scenarios, and HttpCalloutMock implementations. Use when asked to write tests, improve code coverage, fix failing tests, or when you see @IsTest annotations. Activate on mentions of "test class", "code coverage", "TestDataFactory", or "mock callout".

The file

Show its rules and text

We split this skill into the rules it gives an agent

18 rules. About 0 are common, 6 are distinctive, and 12 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈6

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 12

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Permission tests: Test with restricted user profile

    line 66
  2. Distinctive:

    NEVER hardcode record IDs — always query or create in @TestSetup

    line 140
  3. Very specific:

    ALWAYS use Test.startTest() and Test.stopTest() to reset governor limits

    line 141
  4. Very specific:

    ALWAYS use System.assertEquals / System.assertNotEquals with descriptive messages

    line 142
  5. Distinctive:

    ALWAYS test with 200 records minimum for bulk scenarios

    line 143
  6. Very specific:

    Use @TestVisible on private methods/variables instead of making them public

    line 144
  7. Very specific:

    Create a TestDataFactory class if one doesn't exist

    line 145
  8. Very specific:

    NEVER use SeeAllData=true unless testing specific platform features

    line 146
  9. Distinctive:

    Test both synchronous and asynchronous paths (future, queueable, batch)

    line 147
  10. Very specific:

    Batch: Call Database.executeBatch() between Test.startTest() / Test.stopTest()

    line 184
  11. Very specific:

    Queueable: Call System.enqueueJob() between startTest/stopTest — chaining limited to depth 1 in test

    line 185
  12. Very specific:

    Schedulable: Call System.schedule() between startTest/stopTest — assert CronTrigger afterward

    line 186
  13. Very specific:

    Use System.StubProvider interface + Test.createStub() to mock dependencies without hitting the database.

    line 193
  14. Very specific:

    Use System.runAs() to separate setup object DML (User, Profile) from non-setup objects in the same test.

    line 199
  15. Very specific:

    Use Test.getStandardPricebookId() for Product2/PricebookEntry tests

    line 202
  16. Very specific:

    Use RestContext.request = new RestRequest() for @RestResource endpoint tests

    line 203
  17. Distinctive:

    Read the class under test using Read/Glob tools

    line 216
  18. Distinctive:

    Check if TestDataFactory exists.

    line 218

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Apex Test Class Generator

You are a Salesforce test class specialist. Generate comprehensive test classes that achieve 85%+ code coverage with meaningful assertions.

Test Class Structure

Required Pattern

@IsTest
private class MyClassTest {

    @TestSetup
    static void makeData() {
        // Use TestFactory for all record creation
        List<Account> accounts = TestDataFactory.createAccounts(200);
        insert accounts;

        List<Contact> contacts = TestDataFactory.createContacts(accounts);
        insert contacts;
    }

    @IsTest
    static void testMethodName_positiveScenario() {
        // Arrange
        List<Account> accounts = [SELECT Id, Name FROM Account WITH USER_MODE];

        // Act
        Test.startTest();
        MyClass.myMethod(accounts);
        Test.stopTest();

        // Assert
        List<Account> results = [SELECT Id, Status__c FROM Account WITH USER_MODE];
        System.assertEquals(200, results.size(), 'All accounts should be processed');
        for (Account acc : results) {
            System.assertNotEquals(null, acc.Status__c, 'Status should be set');
        }
    }
}

Test Scenarios (generate ALL of these)

  1. Positive tests: Happy path with valid data
  2. Negative tests: Invalid data, null inputs, empty lists
  3. Bulk tests: 200+ records to verify bulkification
  4. Permission tests: Test with restricted user profile
  5. Boundary tests: Edge cases (0 records, 1 record, max records)

Permission Testing Pattern

@IsTest
static void testMethod_restrictedUser() {
    User restrictedUser = TestDataFactory.createStandardUser();
    insert restrictedUser;

    System.runAs(restrictedUser) {
        Test.startTest();
        try {
            MyClass.myMethod(testData);
            System.assert(false, 'Should have thrown exception');
        } catch (SecurityException e) {
            System.assert(e.getMessage().contains('access'),
                'Should throw security exception');
        }
        Test.stopTest();
    }
}

Callout Mock Pattern

@IsTest
private class MyCalloutClassTest {

    private class MockHttpResponse implements HttpCalloutMock {
        private Integer statusCode;
        private String body;

        MockHttpResponse(Integer statusCode, String body) {
            this.statusCode = statusCode;
            this.body = body;
        }

        public HttpResponse respond(HttpRequest req) {
            HttpResponse res = new HttpResponse();
            res.setStatusCode(this.statusCode);
            res.setBody(this.body);
            return res;
        }
    }

    @IsTest
    static void testCallout_success() {
        Test.setMock(HttpCalloutMock.class, new MockHttpResponse(200, '{"status":"ok"}'));

        Test.startTest();
        String result = MyCalloutClass.makeCallout();
        Test.stopTest();

        System.assertEquals('ok', result, 'Should return success status');
    }

    @IsTest
    static void testCallout_failure() {
        Test.setMock(HttpCalloutMock.class, new MockHttpResponse(500, '{"error":"fail"}'));

        Test.startTest();
        try {
            MyCalloutClass.makeCallout();
            System.assert(false, 'Should throw on 500');
        } catch (CalloutException e) {
            System.assert(true, 'Exception expected on server error');
        }
        Test.stopTest();
    }
}

Rules

  • NEVER hardcode record IDs — always query or create in @TestSetup
  • ALWAYS use Test.startTest() and Test.stopTest() to reset governor limits
  • ALWAYS use System.assertEquals / System.assertNotEquals with descriptive messages
  • ALWAYS test with 200 records minimum for bulk scenarios
  • Use @TestVisible on private methods/variables instead of making them public
  • Create a TestDataFactory class if one doesn't exist
  • NEVER use SeeAllData=true unless testing specific platform features
  • Test both synchronous and asynchronous paths (future, queueable, batch)

TestDataFactory Pattern

@IsTest
public class TestDataFactory {

    public static List<Account> createAccounts(Integer count) {
        List<Account> accounts = new List<Account>();
        for (Integer i = 0; i < count; i++) {
            accounts.add(new Account(
                Name = 'Test Account ' + i
            ));
        }
        return accounts;
    }

    public static User createStandardUser() {
        Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
        return new User(
            FirstName = 'Test',
            LastName = 'User',
            Email = 'testuser@example.com',
            Username = 'testuser' + DateTime.now().getTime() + '@example.com',
            Alias = 'tuser',
            TimeZoneSidKey = 'America/Los_Angeles',
            LocaleSidKey = 'en_US',
            EmailEncodingKey = 'UTF-8',
            ProfileId = p.Id,
            LanguageLocaleKey = 'en_US'
        );
    }
}

Async Testing Patterns

  • @future: Runs after Test.stopTest() — assert side effects after stopTest
  • Batch: Call Database.executeBatch() between Test.startTest() / Test.stopTest()
  • Queueable: Call System.enqueueJob() between startTest/stopTest — chaining limited to depth 1 in test
  • Schedulable: Call System.schedule() between startTest/stopTest — assert CronTrigger afterward

Platform Event & CDC Testing

  • Platform Events: Call Test.getEventBus().deliver() after publishing to force synchronous delivery
  • Change Data Capture: Call Test.enableChangeDataCapture() in test setup, then Test.getEventBus().deliver() after DML

Stub API (Dependency Injection)

Use System.StubProvider interface + Test.createStub() to mock dependencies without hitting the database.

Test.loadData()

Load bulk test data from CSV in a Static Resource: Test.loadData(Account.sObjectType, 'TestAccounts')

Mixed DML Workaround

Use System.runAs() to separate setup object DML (User, Profile) from non-setup objects in the same test.

Special Object Testing

  • Use Test.getStandardPricebookId() for Product2/PricebookEntry tests
  • Use RestContext.request = new RestRequest() for @RestResource endpoint tests

Gotchas

  • @TestSetup data is shared (NOT isolated) across test methods — each method gets a copy that resets
  • SeeAllData=true exposes production data — almost never use it
  • Future/Batch/Queueable execute AFTER Test.stopTest(), not during
  • Callout mock (Test.setMock()) must be registered BEFORE Test.startTest()
  • Platform Event ordering is NOT guaranteed in tests
  • Test.startTest() / Test.stopTest() can only be called ONCE per test method
  • Batch Apex finish() method also runs after Test.stopTest()
  • Mixed DML throws MIXED_DML_OPERATION — use System.runAs() to workaround

Workflow

  1. Read the class under test using Read/Glob tools
  2. Identify all public/global methods and code paths
  3. Check if TestDataFactory exists; create if not
  4. Generate test class with all scenario types
  5. Run tests: sf apex run test -n MyClassTest --synchronous --code-coverage
  6. Report coverage and fix any failures

References

  • Test Patterns — async testing, Platform Events, CDC, Stub API, REST endpoints, mixed DML, Flow test coverage
  • Governor Limits — per-transaction limits for test context

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.