grzmol/vibe-force/sf-fflib-testing
Claude Code and agent skill by grzmol/vibe-force
- community
Checking that the SKILL.md file is still in the repository.
Covers every testing variant available in an Apex Enterprise Patterns (fflib) codebase - ApexMocks stub lifecycle (fflib_ApexMocks, startStubbing/when/thenReturn/stopStubbing, verify and verification modes), argument matchers and fflib_ArgumentCaptor, mock injection through Application.UnitOfWork/Service/Selector/Domain setMock, fake SObject graphs with fflib_ApexMocksUtils and fflib_IDGenerator, DML-free domain trigger tests via fflib_SObjectDomain.Test.Database, real-DML selector and integration tests, async/Queueable/Batch/Schedulable tests, platform-event delivery with Test.getEventBus().deliver(), callout mocks, and FLS/CRUD negative tests. Use when writing or reviewing any Apex test in a repo containing fflib_ApexMocks, fflib_SObjectSelector, fflib_SObjectDomain, fflib_SObjectUnitOfWork or an Application class, when a mock verification fails, when choosing between a mocked unit test and a real-DML integration test, or when Apex coverage gates (apexOrgCoverageMin 85 / apexClassCoverageMin 75) must be met without writing coverage-padding tests.
Install
npx vskill@latest install grzmol/vibe-force/sf-fflib-testingSKILL.md
The same SKILL.md was published earlier as grzmol/vibe-force/sf-fflib-testing.
fflib Testing
Testing an fflib codebase means choosing, per class under test, between a mocked unit test (fast, proves collaboration, proves nothing about the org) and a real-DML test (slow, proves the platform actually behaves). Getting that choice wrong is the single largest source of worthless Apex test suites: 90% coverage where every selector's SOQL is unproven.
Framework grounding: fflib-apex-mocks @ master commit d81e9e1 and fflib-apex-common @ master commit dab5977 (both apex-enterprise-patterns). Platform grounding: Apex Developer Guide, Summer '26 / API version 67.0 (doc version 262.0) - the apiVersion in config/vibe-force.defaults.json.
Read sf-fflib-foundations for the Application/factory wiring these tests inject into, sf-fflib-selector-layer for selector construction, sf-fflib-domain-service-uow for the layers under test, and sf-fflib-operations for CI/packaging of the framework itself.
When to use
Use this skill when a repo contains fflib_ApexMocks, fflib_SObjectDomain, fflib_SObjectSelector, fflib_SObjectUnitOfWork, or an Application class, and you are:
- writing a new test for a service, domain, selector, controller, trigger, or async job;
- diagnosing an
fflib_ApexMocks.ApexMocksExceptionfromverify(...)orwhen(...); - deciding whether a behaviour needs a mock or real DML;
- raising coverage to clear the
apexgate without adding tests that assert nothing.
For non-fflib Apex testing fundamentals (@IsTest, @TestSetup, System.runAs, Assert) see sf-apex-testing. For LWC component tests see sf-lwc-jest-testing. For limit budgets consumed by real-DML tests see sf-governor-limits.
Test-variant decision table
| # | Variant | What it proves | What it cannot prove | Speed | Org data | fflib machinery | Coverage contribution | Typical failure mode | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | Service unit test with mocks | Service orchestration: which selector/domain/UoW calls happen, in what shape | SOQL validity, triggers, FLS, validation rules | ~10-40 ms | none | fflib_ApexMocks, Application.*.setMock, fflib_Match | Service impl class only | Over-specified verify breaks on harmless refactor | |
| 2 | Domain unit test, no DML, direct construction | Pure field logic on in-memory records | Trigger routing, save order | ~5-20 ms | none | fflib_IDGenerator for ids; no mocks needed | Domain class | Asserting through the UoW mock instead of the record | |
| 3 | Domain trigger test via fflib_SObjectDomain.Test.Database | onApplyDefaults/onValidate/onBefore*/onAfter* routing and error registration | Real DML errors, validation rules, roll-ups, chained triggers | ~10-30 ms | none | fflib_SObjectDomain.Test.Database.onInsert/onUpdate/onDelete/onUndelete + fflib_SObjectDomain.triggerHandler(Type), fflib_SObjectDomain.Errors | Domain + trigger handler | Forgetting Errors.clearAll() between assertions in one method | |
| 4 | Selector test against real data | The SOQL compiles, field list is right, filters and ordering work | Behaviour of callers | 200 ms - 2 s | inserted records | Real selector, no mocks, fflib_QueryFactory output | Selector class | Mocking the selector - proves nothing | |
| 5 | Service integration test with real DML | End-to-end: service + domain + selector + UoW + triggers + validation | Isolation of a single failure cause | 0.5-5 s | inserted records | Application.UnitOfWork.newInstance(), no setMock | Every layer it crosses | Slow suite; used as the only test style | |
| 6 | Trigger test with real DML | Save order, recursion, cross-object roll-ups, validation rules | Nothing about isolated units | 0.5-3 s | inserted records | Real trigger + fflib_SObjectDomain.triggerHandler | Trigger + domain | Assumes @TestSetup data survives Test.startTest() limits reset | |
| 7 | Controller / @AuraEnabled test | Wire adapter contract: return shape, AuraHandledException messages | Client rendering | ~20-80 ms mocked | none if service mocked | Application.Service.setMock(IFooService.class, mock) | Controller class | Testing the service again through the controller | |
| 8 | Flow-invocable test (@InvocableMethod) | Bulk request/response contract and per-row mapping | Flow itself | ~20-80 ms | none if service mocked | Application.Service.setMock | Invocable class | Single-row-only test on a bulk API | |
| 9 | Async test: Queueable / Schedulable | The job enqueues, runs, and commits inside Test.startTest()/stopTest() | Behaviour under real 2-minute chaining/limits | 0.5-3 s | usually yes | Real UoW; mock the service the job calls where possible | Job class | Asserting before Test.stopTest() | |
| 10 | Async test: Batchable | start/execute/finish wiring, one chunk | Multi-chunk state, real scope sizes | 1-5 s | yes | Batchable classes cannot be stubbed (Stub API limit) - mock its collaborators instead | Batch class | Expecting finish after an unhandled execute exception in test context | |
| 11 | Platform-event test | Event publication and the subscriber trigger | Real bus latency/replay | 0.3-2 s | sometimes | uow.registerPublishAfterSuccessTransaction, then Test.getEventBus().deliver() | Publisher + subscriber | Missing deliver(), so the subscriber never runs | |
| 12 | Callout test | Request shape and response handling | The remote system | ~30-200 ms | none | Test.setMock(HttpCalloutMock.class, ...) inside the mocked service boundary | Callout class | Callout mock set after the callout is triggered | |
| 13 | FLS / CRUD negative test | USER_MODE DML and selector security actually reject | Anything under system mode | 0.5-3 s | user + perm set | fflib_SObjectUnitOfWork.UserModeDML, System.runAs, Assert.fail() + catch (SecurityException) | Security paths | Running as a System Administrator, so nothing is denied | |
| 14 | LWC Jest test | Client behaviour, wire handling, DOM | Any Apex | ~50-300 ms | none | none (see sf-lwc-jest-testing) | JS coverage (jestCoverageMin) | Duplicating Apex assertions in JS |
Rows 1-3, 7, 8 are the fast tier; rows 4-6, 9-13 are the slow tier. A healthy fflib org is roughly 70% fast tier / 25% slow tier / 5% end-to-end, described under "Test pyramid" below.
Core patterns
1. The ApexMocks lifecycle
fflib_ApexMocks implements System.StubProvider; mocks.mock(Type) returns Test.createStub(classToMock, this). The stub is named <Type>__sfdc_ApexStub, which is what you see in failure messages.
Rules that follow from the source:
- Every `when(...)` must be inside `startStubbing()`/`stopStubbing()`. Outside it, the call is
recorded as a real invocation and then verified against later, corrupting counts.
- **
when(...)must be followed bythenReturn/thenThrow/thenAnswer/thenReturnMulti/
thenThrowMulti.** A bare when(...) yields ApexMocksException: The stubbing is not correct, no return values have been set. at call time.
- Unstubbed methods return `null`, they do not throw. A
NullPointerExceptiondeep inside the
class under test almost always means "you forgot to stub something".
- `mocks.mock()` uses the Stub API, so the platform's stub limitations apply: no static or
private methods, no properties, no triggers, no inner classes, no system types, no Batchable implementers, no classes with only private constructors, and iterators cannot be a parameter or return type. The mocked type must be in the same namespace as the Test.createStub() call. (Apex Developer Guide, Build a Mocking Framework with the Stub API)
2. Generated mocks vs Stub API mocks
Two modes exist. Only the first is current.
| Mode | How a mock is made | Needs regeneration on interface change | Use | |
|---|---|---|---|---|
| Stub API (current) | (IFoo) mocks.mock(IFoo.class) -> Test.createStub | No | Default for everything | |
| ApexMocks Generator (legacy) | Pre-generated FooMocks.Foo class implementing the interface and delegating to mocks.mockNonVoidMethod(...) / mocks.mockVoidMethod(...) | Yes - stale generated mocks silently drift | Only for types the Stub API refuses to stub |
mockVoidMethod(Object, String, List<Type>, List<Object>) and mockNonVoidMethod(...) remain public on fflib_ApexMocks for generated classes. The generator itself is an external tool, not part of the fflib-apex-mocks repo.
fflib_ApexMocksConfig.HasIndependentMocks (default false) controls whether two mock instances of the same type share stubbing and counts. fflib_QualifiedMethod.equals/hashCode include the mock instance only when it is true. Set it to true in @IsTest setup when a single test holds two mocks of one interface and must distinguish them:
3. Verification API
This is the start of the file. Read the full SKILL.md on GitHub.
Security
- Security scanPassed, 95 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-fflib-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-fflib-testing)