Testing Apex Code

Share

Test Classes & Test Methods

Why Testing Matters in Salesforce

In Salesforce, you can’t deploy Apex to production safely without tests. Tests ensure your code works and doesn’t break other features.

Simple Explanation

Tests are like a safety check before a car goes on the road.


What Is a Test Class?

A test class contains test methods that validate your Apex logic.

Basic Test Class Example

@isTest
private class AccountServiceTest {
    @isTest
    static void testCreateAccount() {
        Account acc = new Account(Name = 'Test Account');
        insert acc;

        Account saved = [SELECT Name FROM Account WHERE Id = :acc.Id];
        System.assertEquals('Test Account', saved.Name);
    }
}

Key points

  • @isTest makes it a test class/method

  • Test data is created inside the test

  • Use assertions to verify results


Gist (Quick Revision)

Test classes validate your code by creating test data, running logic, and checking results.


@isTest and @testSetup

What Does @isTest Do?

@isTest tells Salesforce:

  • This code is for testing only

  • It doesn’t count against org code limits (in most cases)

  • It can run without affecting production data


What Is @testSetup?

@testSetup creates shared test data once for all methods in the test class.

Real-Life Example

Instead of cooking rice 5 times for 5 people, cook once and share.


Example Using @testSetup

@isTest
private class AccountTests {

    @testSetup
    static void setupData() {
        insert new Account(Name = 'Setup Account');
    }

    @isTest
    static void testAccountExists() {
        Account acc = [SELECT Id, Name FROM Account WHERE Name = 'Setup Account' LIMIT 1];
        System.assertNotEquals(null, acc.Id);
    }
}

Gist (Quick Revision)

Use @testSetup to create common test records once and reuse them across tests.


Assertions & Negative Testing

What Are Assertions?

Assertions confirm expected outcomes.

Common assertions:

  • System.assert(condition)

  • System.assertEquals(expected, actual)

  • System.assertNotEquals(notExpected, actual)


Assertion Example

System.assertEquals('Setup Account', acc.Name);

What Is Negative Testing?

Negative testing ensures your code behaves correctly when something goes wrong.

Real-Life Example

Testing that a door doesn’t open without a key.


Negative Test Example (Expecting an Error)

@isTest
static void testMissingNameShouldFail() {
    try {
        insert new Account(); // Name missing
        System.assert(false, 'Insert should have failed');
    } catch (DmlException e) {
        System.assert(e.getMessage().contains('Required'), 'Expected required field error');
    }
}

Gist (Quick Revision)

Assertions verify success; negative tests verify correct failure behavior.


Code Coverage Strategy

What Is Code Coverage?

Code coverage means:

How much of your Apex code is executed by tests.

Salesforce requires at least 75% Apex code coverage to deploy to production.

Important Note

Coverage alone is not enough—tests must be meaningful and validate outcomes.


Good Coverage Strategy (Career-Ready)

✅ Focus on:

  • Business logic (service classes)

  • Trigger handlers

  • Validation paths (both success and failure)

  • Edge cases

✅ Best practice:

  • Write tests for each method

  • Test both happy path and error path


Quick Example Strategy

If a method has:

  • 1 success path

  • 2 failure paths
    Your test should cover all 3.


Gist (Quick Revision)

Aim for coverage, but prioritize high-quality tests that validate real behavior.

  • January 6, 2026