grzmol/vibe-force/sf-apex-testing
Claude Code and agent skill by grzmol/vibe-force
- community
Checking that the SKILL.md file is still in the repository.
Writes and repairs Salesforce Apex unit tests — @IsTest classes and methods, @TestSetup, Test.startTest/Test.stopTest limit contexts, System.runAs for user-context and FLS/CRUD negative tests, the modern Assert class (Assert.areEqual, isTrue, isNull, isInstanceOfType, fail) versus legacy System.assertEquals, test data factory and builder patterns, Test.loadData with static resources, HttpCalloutMock, StaticResourceCalloutMock, MultiStaticResourceCalloutMock, WebServiceMock, Test.setMock, the Stub API (System.StubProvider and Test.createStub), asynchronous testing of Queueable, Batch, future and Schedulable jobs, Test.getEventBus().deliver() for platform events, Test.enqueueBatchJobs, 200-record bulk trigger tests, code coverage rules and the 75% deployment requirement, sf apex run test flag selection, test suites, flaky-test diagnosis, and how vf-check apex gates apexOrgCoverageMin and apexClassCoverageMin. Use this skill when creating or fixing a *Test.cls, when coverage or a deployment test level blocks a release, when a test is flaky or org-data dependent, or before running vf-check apex or sf apex run test.
Install
npx vskill@latest install grzmol/vibe-force/sf-apex-testingSKILL.md
The same SKILL.md was published earlier as grzmol/vibe-force/sf-apex-testing.
Apex Testing
Coverage and test-level rules below are platform requirements; the numeric gates are vibe-force policy from .vibeforce/config.json.
| Rule | Value | Source | |
|---|---|---|---|
| Platform minimum coverage to deploy or package Apex | 75% of Apex code, all tests passing | Apex Developer Guide | |
Platform per-class minimum under RunSpecifiedTests | 75% per class and per trigger, computed individually | Salesforce CLI reference | |
| Every trigger must have some coverage | non-negotiable | Apex Developer Guide | |
vibe-force org coverage gate | gates.apexOrgCoverageMin = 85 | config/vibe-force.defaults.json | |
vibe-force per-class coverage gate | gates.apexClassCoverageMin = 75 | config/vibe-force.defaults.json | |
| New Apex class requires a test class | gates.requireTestForApexClass = true | config/vibe-force.defaults.json |
Not counted as covered: System.debug calls, test methods, and test classes. Conditional and ternary operators count as executed only when both branches run.
When to use
- Creating or repairing any
*Test.cls, or a deployment fails its test level. - Coverage is below a gate, or differs between sandbox and production.
- A test is flaky, order-dependent, or reads org data.
- Asynchronous work (Queueable, Batch, future, Schedulable, platform events) needs deterministic
assertions.
- A callout, an external service, or a collaborator class must be mocked.
Adjacent skills: sf-apex-development (code under test), sf-async-apex-patterns (job design), sf-governor-limits (Limits assertions), sf-security-model (permission sets for negative tests), sf-lwc-jest-testing (client tests), sf-deployment-strategies (test levels per environment), sf-debugging-logs (diagnosing a failing test).
Decision table
| Situation | Do this | |
|---|---|---|
| Shared records for every method in the class | @TestSetup static void setup() — rolled back after each test method | |
| Records must exist before the code under test runs | create them before Test.startTest() | |
| Need a clean governor-limit budget for the assertion window | wrap the exercise in Test.startTest() / Test.stopTest() | |
| Async job must complete before assertions | enqueue inside the startTest/stopTest block; assert after stopTest() | |
| Platform event must be delivered | Test.getEventBus().deliver(); after Test.stopTest() | |
| Callout in the code path | Test.setMock(HttpCalloutMock.class, new XMock()); | |
| Collaborator class must return canned data | Stub API: Test.createStub(IFoo.class, provider) | |
Collaborator is static, private, inner, a trigger, or Batchable | Stub API cannot mock it — inject an interface instead | |
| SOSL must return known rows | Test.setFixedSearchResults(ids) — SOSL otherwise returns empty in tests | |
| Verify behaviour for a restricted user | System.runAs(u) { ... } with a permission-set-assigned user | |
| Mixed DML (setup object plus normal object) | wrap in System.runAs(thisUser) { ... } | |
Field is not writable from Apex (e.g. CreatedDate) | Test.setCreatedDate(recordId, dt) | |
| Must read existing org data | avoid; if truly unavoidable @IsTest(SeeAllData=true) and document why | |
| Bulk behaviour | 200 records — the documented Apex trigger batch size |
Core patterns
1. Test class anatomy
@IsTest classes must be top-level, cannot be interfaces or enums, and their access modifiers are irrelevant — the framework finds the methods regardless. @IsTest code is excluded from the 6 MB org code limit and from coverage arithmetic. Static member values changed in a @TestSetup or test method are not preserved for other methods in the class. @TestSetup is unsupported in a class annotated @IsTest(SeeAllData=true).
2. Test.startTest / Test.stopTest semantics
Both may be called at most once per test method.
| Behaviour | Detail | |
|---|---|---|
| Limit context | code between the two calls receives a new set of governor limits; startTest adds a context rather than refreshing it, so limits consumed before it are restored after stopTest | |
| Asynchronous flush | all async calls made after startTest are collected and run synchronously at stopTest | |
| Exception during flush | halts the synchronous processing — for example an unhandled exception in a batch execute prevents finish from running | |
| Savepoints | from API 60.0, all savepoints are released when startTest and stopTest are called; a SAVEPOINT_RESET event is logged |
Arrange before startTest, act between, assert after stopTest.
3. Assertions
Use the Assert class. System.assert* still compiles but carries no message discipline and is legacy.
| Legacy | Modern | |
|---|---|---|
System.assertEquals(a, b) | Assert.areEqual(a, b, 'why') | |
System.assertNotEquals(a, b) | Assert.areNotEqual(a, b, 'why') | |
System.assert(cond) | Assert.isTrue(cond, 'why') | |
System.assert(!cond) | Assert.isFalse(cond, 'why') | |
System.assert(x == null) | Assert.isNull(x, 'why') | |
System.assert(x != null) | Assert.isNotNull(x, 'why') | |
System.assert(false, 'unreachable') | Assert.fail('DmlException expected') | |
| no equivalent | Assert.isInstanceOfType(o, Account.class) / Assert.isNotInstanceOfType(...) |
Argument order is (expected, actual, message). Negative tests assert the failure, not merely that something threw:
Full Assert API table in references/assertions-and-mocks.md.
4. Test data factory
Public @IsTest utility classes are excluded from the org code limit and callable only from test context. Build records; never read org data.
Tests are isolated from org data by default — @IsTest(SeeAllData=false) is the implicit behaviour for API 24.0 and later. SeeAllData=true at class level cannot be overridden by a method-level false, and it is incompatible with IsParallel=true. Builders, Test.loadData with static resources, and Test.setCreatedDate recipes are in references/test-patterns.md.
5. Mocking callouts and collaborators
Tests do not support HTTP callouts; without a mock the test fails.
Mock selection:
| Need | Mock | |
|---|---|---|
| One endpoint, logic in Apex | HttpCalloutMock implementation | |
| Response body held as a static resource | StaticResourceCalloutMock | |
| Several endpoints, each with its own static resource | MultiStaticResourceCalloutMock | |
| SOAP callout from a WSDL2Apex stub | WebServiceMock | |
| Collaborator Apex class | Stub API — System.StubProvider plus Test.createStub |
Stub API limitations shape design: it cannot mock static methods (including future methods), private methods, properties, triggers, inner classes, system types, classes implementing Batchable, or classes with only private constructors; iterators cannot be return or parameter types; and the mocked object must live in the same namespace as the Test.createStub() call. A multi-endpoint router and a full StubProvider are in references/assertions-and-mocks.md.
6. Asynchronous tests
| Async feature | Test technique | Constraint | |
|---|---|---|---|
| Queueable | System.enqueueJob inside startTest/stopTest | chaining is not executed in tests beyond the first job | |
| Batch Apex | Database.executeBatch inside the block | max 5 batch jobs submitted per running test; only one execute chunk runs | |
| Future method | call the method inside the block | @Future cannot be invoked from another @Future | |
| Schedulable | System.schedule inside the block; job runs at stopTest | cron string still required | |
| Flex queue ordering | Test.enqueueBatchJobs(n) plus Test.getFlexQueueOrder() | enqueues no-operation jobs for ordering assertions | |
| Platform events | Test.getEventBus().deliver(); after Test.stopTest() | one deliver() per downstream publishing hop | |
BatchApexErrorEvent | Database.RaisesPlatformEvents on the batch class, then deliver() | wrap the block in try/catch to swallow the job's exception |
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingskills/sf-apex-testing/SKILL.md
Popularity
- 7-day trend score
- 0
Not refreshed yet.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/grzmol/vibe-force/sf-apex-testing)