Sub topic 8
Testing and deployment compared
This chapter assumes you can read basic Apex, have built a simple record-triggered flow, and know what a sandbox and a change set are. It compares how Flow and Apex are tested and moved to production. These are the places where the two tools differ most in day-to-day work.
Testing Apex
Apex has a built-in test framework. A test class is annotated with @IsTest. It creates its own data, runs the code under test, and checks the results with assertions. Tests do not see your org's real records unless you opt in, so they are repeatable.
This test inserts 200 contacts in one operation, which is the size of a single trigger batch. It checks the trigger from the earlier chapters in bulk.
@IsTest
private class ContactTriggerTest {
@IsTest
static void countsContactsInBulk() {
Account a = new Account(Name = 'Acme');
insert a;
List<Contact> contacts = new List<Contact>();
for (Integer i = 0; i < 200; i++) {
contacts.add(new Contact(LastName = 'Contact ' + i, AccountId = a.Id));
}
insert contacts;
a = [SELECT Contact_Count__c FROM Account WHERE Id = :a.Id];
Assert.areEqual(200, a.Contact_Count__c);
}
}The test assumes the custom field Contact_Count__c exists on Account. The 200-record insert makes this more than a coverage exercise. If the trigger ran a query inside its loop, this test would hit a governor limit and fail.
Testing Flow
Flow has two main ways to check behavior before release:
- Debug runs in Flow Builder. You pick a record or enter input values and watch each element execute. Debug runs can save real changes unless you choose the rollback option.
- Flow tests for record-triggered flows. You define the starting record, the field values, and assertions about the outcome. They run in the same transaction style as a real save and can be rerun after each change.
Flow tests exist, but they are narrower than Apex tests. You build them one scenario at a time in the builder, and they are optional. Many teams skip them and rely on manual debugging, so regressions often appear only after someone edits the flow later.
Deployment rules
Apex and Flow differ most at the production gate.
| Topic | Apex | Flow |
|---|---|---|
| Test requirement | Required for production deployment | Not required |
| Coverage rule | At least 75% overall Apex coverage, and every trigger needs some coverage | None |
| Editing in production | Not allowed. Code changes arrive by deployment | Possible, but best avoided |
| Versioning | One current version per class or trigger, tracked in source control | Each save creates a new flow version. Only one version is active |
| Activation | Active as soon as it is deployed | Deployed as inactive unless your org allows flows to deploy as active |
The activation difference matters. A deployed flow can sit inactive and do nothing until someone turns it on. Check the Process Automation Settings in Setup to see how your org handles this.
What this means in practice
Apex gives you stronger safety checks. The platform forces tests to exist, and the tests can exercise bulk behavior and failure paths. Fixing a bug means a code change, a test run, and a deployment, which is slower but well controlled.
Flow gives you faster delivery. An admin can change a flow and test it in minutes. The cost is that nothing forces a regression check. If you use Flow for important logic, add flow tests and keep a short manual test script for the scenarios that matter most.
When a requirement is both critical and complex, such as financial calculations or logic with many branches, the testing gap is a good reason to move that part into Apex. A flow can still call it, as a later chapter shows.
Exercise
Your team deploys a new Apex trigger to production, and the deployment fails even though the trigger works in the sandbox. Give two testing-related reasons this could happen. Then say what a deployment of only a new, tested flow would not be blocked by.
Show answer
Two possible reasons are that overall Apex coverage in the org is below 75%, or that the trigger has no test coverage at all. Tests that pass in a sandbox can also fail in production if they depend on data or settings that only exist in the sandbox. A flow deployment is not blocked by a coverage requirement, because Flow has no coverage rule. The flow may arrive inactive, so it would also need to be activated.