Intermediate 30 min read

Test the Feature

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

  • Manually verify the feature's behavior using Execute Anonymous
  • Walk through both the happy path and the validation-blocking scenario
  • Recognize this as informal verification, not a substitute for the formal unit tests covered later

Prerequisites: "Add Service Classes"

A quick note on what "testing" means here

This course hasn't yet covered @isTest and Apex's formal unit-testing tools — that's the entire subject of a later Testing module. This lesson verifies the feature manually, the same way you'd sanity-check it while actively building it: running realistic scenarios through Execute Anonymous (Module 1) and inspecting the results directly.

Verifying the happy path

Account acc = new Account(Name = 'Test Account');
insert acc;

Opportunity opp = new Opportunity(
    Name = 'Test Deal', AccountId = acc.Id, StageName = 'Prospecting',
    CloseDate = Date.today(), Amount = 50000
);
insert opp;

opp.StageName = 'Closed Won';
update opp;

Account updated = [SELECT Total_Won_Revenue__c FROM Account WHERE Id = :acc.Id];
System.debug('Expected 50000, got: ' + updated.Total_Won_Revenue__c);

Running this in Execute Anonymous and checking the debug log confirms the rollup actually happened correctly — the closest thing to a real end-to-end check available before formal unit tests are introduced.

Verifying the validation blocks correctly

Opportunity badOpp = new Opportunity(
    Name = 'Bad Deal', AccountId = acc.Id, StageName = 'Prospecting',
    CloseDate = Date.today() // no Amount set
);
insert badOpp;

try {
    badOpp.StageName = 'Closed Won';
    update badOpp;
    System.debug('ERROR: this should have been blocked!');
} catch (DmlException e) {
    System.debug('Correctly blocked: ' + e.getMessage());
}

This is Module 19's try/catch around a DmlException, used here specifically to confirm the Lesson 4 validation actually fires — the "correctly blocked" message means addError() is working as designed.

Exercise

Write an Execute Anonymous snippet that moves an Opportunity from Closed Won back to Prospecting and checks that the Account's Total_Won_Revenue__c decreased accordingly.

Show hint

Set StageName back, update, then re-query the Account.

APEX

Test the Feature Quiz

1. Why does this lesson use Execute Anonymous rather than @isTest?

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

Before this course's dedicated Testing module, this lesson verifies the feature the practical way: running real scenarios through Execute Anonymous and checking the actual results.