Advanced 35 min read

Write Comprehensive Tests

By the end of this lesson, you'll be able to:

  • Test each health metric independently with known, verifiable data
  • Test the combined getHealthSummary method
  • Test the controller method end to end

Prerequisites: "Build the Apex Controller for LWC"

Testing one metric with known data

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

        insert new List<Case>{
            new Case(AccountId = acc.Id, Status = 'New'),
            new Case(AccountId = acc.Id, Status = 'New'),
            new Case(AccountId = acc.Id, Status = 'Closed')
        };

        AccountHealthService service = new AccountHealthService();
        System.assertEquals(2, service.getOpenCaseCount(acc.Id));
    }
}

Two open Cases and one closed one, with a specific assertion of 2 — exactly Module 31's "assertions that actually tell you something" habit, verifying the exact expected count, not just "greater than zero."

Testing the combining method

@isTest
static void getHealthSummaryCombinesAllFourMetrics() {
    Account acc = new Account(Name = 'Test Account');
    insert acc;

    insert new Case(AccountId = acc.Id, Status = 'New');

    AccountHealthService service = new AccountHealthService();
    AccountHealthSummary summary = service.getHealthSummary(acc.Id);

    System.assertEquals(1, summary.openCaseCount);
    System.assertNotEquals(null, summary.averageResolutionDays);
    System.assertNotEquals(null, summary.totalWonRevenue);
}

This confirms getHealthSummary actually populates every field on the wrapper, not just that it runs without an exception.

Testing the controller end to end

@isTest
private class AccountHealthControllerTest {
    @isTest
    static void getAccountHealthReturnsExpectedSummary() {
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        insert new Case(AccountId = acc.Id, Status = 'New');

        Test.startTest();
        AccountHealthSummary summary = AccountHealthController.getAccountHealth(acc.Id);
        Test.stopTest();

        System.assertEquals(1, summary.openCaseCount);
    }
}

Calling AccountHealthController.getAccountHealth directly — exactly as an LWC would — confirms the entire chain (controller → service → SOQL) works correctly end to end, not just the service layer in isolation.

Exercise

Write a test confirming getTotalWonRevenue returns 0 (not null) for an Account with no Total_Won_Revenue__c set.

Show hint

Insert an Account without setting that field, then check the returned value is 0.

APEX

Write Comprehensive Tests Quiz

1. Why does the controller test call AccountHealthController.getAccountHealth directly, rather than only testing AccountHealthService?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

Following Module 32's established rhythm, this lesson writes formal test coverage for the service layer, the combining method, and the controller — each with specific, verifiable assertions.