Sub topic 1
Apex Tests as a Deployment Gate
Prerequisites
This chapter assumes you know basic Apex syntax and the @IsTest annotation. You should also know the difference between a sandbox and production, and be able to run sf commands. Later chapters cover each test level in detail. This one explains why tests sit in the deployment path at all.
Why deployments run tests
Salesforce is a multitenant platform, so many customers share the same infrastructure. Salesforce wants to be sure that Apex you deploy does not break existing behavior. It uses Apex tests as the check. When you deploy to production, the platform can run tests and block the deployment if they fail or if coverage is too low.
In this sense, the tests act as a gate. Your metadata only reaches the org if the tests pass. If a test fails, the deployment fails and nothing is saved.
What a deployable test looks like
A test class is an Apex class marked @IsTest. Each test method creates its own data, runs the code under test, and asserts a result. Tests do not see existing org data by default, so the data they need must be created inside the test.
public with sharing class AccountService {
public static Integer countByIndustry(String industry) {
return [SELECT COUNT() FROM Account WHERE Industry = :industry];
}
}
@IsTest
private class AccountServiceTest {
@IsTest
static void countsMatchingAccounts() {
insert new Account(Name = 'Acme', Industry = 'Energy');
Test.startTest();
Integer result = AccountService.countByIndustry('Energy');
Test.stopTest();
Assert.areEqual(1, result, 'Expected one Energy account');
}
}The test inserts its own Account, so it does not depend on what is in the org. It calls the method between Test.startTest() and Test.stopTest(), which gives the code a fresh set of governor limits. The Assert.areEqual call fails the test if the count is wrong. A test with no assertion can still execute lines and raise coverage, but it proves nothing about behavior.
Tests and coverage
Running a test also records which lines of Apex executed. Salesforce turns that record into code coverage, the percentage of lines in your classes and triggers that ran during tests. Production deployments that include Apex must meet a coverage threshold. The next chapter explains how coverage is measured. For now, remember that tests do two jobs in a deployment:
- They prove that the code behaves as expected.
- They produce the coverage numbers that the deployment checks.
Where the gate sits
Which tests run in step B depends on the test level. There are four levels, and they differ in how many tests run and which rules apply. The levels are the subject of the chapters that follow.
Common mistakes
- Relying on org data. A test that passes in a sandbox with copied data can fail in production, where that data is missing. Create the data inside the test.
- Writing tests only for coverage. Without assertions, a test raises coverage but will not catch a regression.
- Treating the sandbox as a fair preview of production. Sandbox deployments do not require tests by default, so a class with no working test can deploy there and then fail when you deploy to production.
Exercise
A teammate says a test method that calls your class but has no assertions is fine, because the deployment still passes the coverage check. What is wrong with that view?
Show answer
Coverage only records which lines ran. It does not check results. A test with no assertions can execute lines and raise coverage, but it will not fail when the behavior is wrong, so it does not protect the org from regressions. The test should assert the expected outcome.