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));
    }
}
  1. @isTest — marks the test class.
  2. private class AccountHealthServiceTest { — a dedicated test class for the service.
  3. @isTest — marks the test method.
  4. static void openCaseCountReflectsOnlyOpenCases() { — a name that states exactly what's being verified.
  5. Account acc = new Account(Name = 'Test Account'); — test data, created fresh.
  6. insert acc; — Module 19's DML, saving it.
  7. (blank line)
  8. insert new List<Case>{ — Module 25's bulk-insert style, three Cases at once.
  9. new Case(AccountId = acc.Id, Status = 'New'), — an open Case.
  10. new Case(AccountId = acc.Id, Status = 'New'), — a second open Case.
  11. new Case(AccountId = acc.Id, Status = 'Closed') — a closed Case, deliberately included to prove it's excluded from the count.
  12. }; — closes the list literal and the insert statement.
  13. (blank line)
  14. AccountHealthService service = new AccountHealthService(); — the class under test.
  15. System.assertEquals(2, service.getOpenCaseCount(acc.Id)); — asserts the exact count, not just "greater than zero."
  16. } — closes the test method.
  17. } — closes the test class.

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);
}
  1. @isTest — marks the test method.
  2. static void getHealthSummaryCombinesAllFourMetrics() { — the name states the whole method's job.
  3. Account acc = new Account(Name = 'Test Account'); — fresh test data.
  4. insert acc; — saves it.
  5. (blank line)
  6. insert new Case(AccountId = acc.Id, Status = 'New'); — one open Case, enough to give the count a non-zero value to check.
  7. (blank line)
  8. AccountHealthService service = new AccountHealthService(); — the class under test.
  9. AccountHealthSummary summary = service.getHealthSummary(acc.Id); — calls the combining method.
  10. (blank line)
  11. System.assertEquals(1, summary.openCaseCount); — the exact expected count.
  12. System.assertNotEquals(null, summary.averageResolutionDays); — confirms the field was actually populated.
  13. System.assertNotEquals(null, summary.totalWonRevenue); — same.
  14. } — closes the test method.

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);
    }
}
  1. @isTest — marks the test class.
  2. private class AccountHealthControllerTest { — a dedicated test class for the controller.
  3. @isTest — marks the test method.
  4. static void getAccountHealthReturnsExpectedSummary() { — states what's verified.
  5. Account acc = new Account(Name = 'Test Account'); — test data.
  6. insert acc; — saves it.
  7. insert new Case(AccountId = acc.Id, Status = 'New'); — one open Case for the summary to reflect.
  8. (blank line)
  9. Test.startTest(); — Module 31's governor-limit reset boundary.
  10. AccountHealthSummary summary = AccountHealthController.getAccountHealth(acc.Id); — calls the controller method directly, exactly as an LWC would, not the service.
  11. Test.stopTest(); — closes the boundary.
  12. (blank line)
  13. System.assertEquals(1, summary.openCaseCount); — confirms the whole chain produced the right answer.
  14. } — closes the test method.
  15. } — closes the test class.

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.