Advanced 30 min read

Test the Feature

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

  • Write an @isTest method verifying a Case routes correctly when a matching rule exists
  • Write an @isTest method verifying the fallback queue applies when no rule matches
  • Write an @isTest method verifying a bulk insert of Cases with mixed Priority/Type routes each one correctly

Prerequisites: "Handle Errors Gracefully"

Verifying a matched rule

@isTest
private class CaseRoutingTest {
    @isTest
    static void caseWithAMatchingRuleRoutesToTheConfiguredQueue() {
        Test.startTest();
        Case c = new Case(Subject = 'Login broken', Priority = 'High', Type = 'Problem');
        insert c;
        Test.stopTest();

        Case saved = [SELECT Assigned_Queue__c FROM Case WHERE Id = :c.Id];
        System.assertNotEquals(null, saved.Assigned_Queue__c, 'Expected a matching High/Problem rule to assign a queue');
    }
}
  1. @isTest — marks the test class.
  2. private class CaseRoutingTest { — the class.
  3. @isTest — marks the test method.
  4. static void caseWithAMatchingRuleRoutesToTheConfiguredQueue() { — a clearly-named test.
  5. Test.startTest(); — marks the start.
  6. Case c = new Case(Subject = 'Login broken', Priority = 'High', Type = 'Problem'); — assuming a Case_Routing_Rule__mdt record exists for High/Problem.
  7. insert c; — fires the trigger.
  8. Test.stopTest(); — marks the end.
  9. (blank line)
  10. Case saved = [SELECT Assigned_Queue__c FROM Case WHERE Id = :c.Id]; — re-queries.
  11. System.assertNotEquals(null, saved.Assigned_Queue__c, 'Expected a matching High/Problem rule to assign a queue'); — confirms the routing lookup actually matched and assigned the configured queue.
  12. } — closes the test method.
  13. } — closes the class.

Verifying the fallback

@isTest
private class CaseRoutingTest {
    // ... caseWithAMatchingRuleRoutesToTheConfiguredQueue() from above ...

    @isTest
    static void caseWithNoMatchingRuleFallsBackToGeneralSupport() {
        Test.startTest();
        Case c = new Case(Subject = 'Unusual request', Priority = 'Low', Type = 'Feature Request');
        insert c;
        Test.stopTest();

        Case saved = [SELECT Assigned_Queue__c FROM Case WHERE Id = :c.Id];
        System.assertEquals('General Support', saved.Assigned_Queue__c, 'Expected the fallback queue when no rule matches');
    }
}
  1. @isTest — marks the class.
  2. private class CaseRoutingTest { — the same test class as before.
  3. // ... caseWithAMatchingRuleRoutesToTheConfiguredQueue() from above ... — the first test method still lives here too.
  4. (blank line)
  5. @isTest — marks a second test method.
  6. static void caseWithNoMatchingRuleFallsBackToGeneralSupport() { — a clearly-named test for the fallback.
  7. Test.startTest(); — marks the start.
  8. Case c = new Case(Subject = 'Unusual request', Priority = 'Low', Type = 'Feature Request'); — choosing a Priority/Type combination deliberately unlikely to have a configured rule.
  9. insert c; — fires the trigger.
  10. Test.stopTest(); — marks the end.
  11. (blank line)
  12. Case saved = [SELECT Assigned_Queue__c FROM Case WHERE Id = :c.Id]; — re-queries.
  13. System.assertEquals('General Support', saved.Assigned_Queue__c, 'Expected the fallback queue when no rule matches'); — confirms Lesson 4's fallback logic actually fires.
  14. } — closes the test method.
  15. } — closes the class.

Verifying a bulk batch with mixed values

@isTest
private class CaseRoutingTest {
    // ... previous test methods ...

    @isTest
    static void bulkInsertWithMixedPriorityAndTypeRoutesEachCaseCorrectly() {
        List<Case> batch = new List<Case>{
            new Case(Subject = 'A', Priority = 'High', Type = 'Problem'),
            new Case(Subject = 'B', Priority = 'Low', Type = 'Question'),
            new Case(Subject = 'C', Priority = 'Medium', Type = 'Problem')
        };

        Test.startTest();
        insert batch;
        Test.stopTest();

        List<Case> saved = [SELECT Subject, Assigned_Queue__c FROM Case WHERE Id IN :batch];
        for (Case c : saved) {
            System.assertNotEquals(null, c.Assigned_Queue__c, 'Expected every Case in the batch to receive a queue assignment: ' + c.Subject);
        }
    }
}
  1. @isTest — marks the class.
  2. private class CaseRoutingTest { — the same test class.
  3. // ... previous test methods ... — the earlier two tests still live here too.
  4. (blank line)
  5. @isTest — marks a third test method.
  6. static void bulkInsertWithMixedPriorityAndTypeRoutesEachCaseCorrectly() { — a clearly-named test for the batch scenario.
  7. List<Case> batch = new List<Case>{ — three Cases with different Priority/Type combinations.
  8. new Case(Subject = 'A', Priority = 'High', Type = 'Problem'), — the first.
  9. new Case(Subject = 'B', Priority = 'Low', Type = 'Question'), — the second.
  10. new Case(Subject = 'C', Priority = 'Medium', Type = 'Problem') — the third.
  11. }; — closes the list literal.
  12. (blank line)
  13. Test.startTest(); — marks the start.
  14. insert batch; — inserts all three together.
  15. Test.stopTest(); — marks the end.
  16. (blank line)
  17. List<Case> saved = [SELECT Subject, Assigned_Queue__c FROM Case WHERE Id IN :batch]; — re-queries all three.
  18. for (Case c : saved) { — loops over the results.
  19. System.assertNotEquals(null, c.Assigned_Queue__c, 'Expected every Case in the batch to receive a queue assignment: ' + c.Subject); — exactly Module 27's "test with multiple records" lesson, applied here — a single-record test could never confirm the bulkified lookup correctly handles several different combinations within the same batch.
  20. } — closes the loop.
  21. } — closes the test method.
  22. } — closes the class.

Exercise

Add a fourth test method, caseWithNullTypeStillGetsAQueueAssignment, that inserts a Case with Priority set but Type left null, and asserts it still receives a queue (the fallback).

Show hint

Leave Type unset on the Case, insert it inside Test.startTest()/stopTest(), then re-query and assert Assigned_Queue__c is not null.

APEX

Test the Feature Quiz

1. Why does this lesson specifically test a bulk batch with mixed Priority/Type values?

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 Modules 26-27's established pattern, this lesson verifies the feature with real @isTest methods — the deeper Testing toolkit (mocks, test data factories, meaningful coverage) is still ahead, in Module 31.