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');
}
}
@isTest— marks the test class.private class CaseRoutingTest {— the class.@isTest— marks the test method.static void caseWithAMatchingRuleRoutesToTheConfiguredQueue() {— a clearly-named test.Test.startTest();— marks the start.Case c = new Case(Subject = 'Login broken', Priority = 'High', Type = 'Problem');— assuming aCase_Routing_Rule__mdtrecord exists forHigh/Problem.insert c;— fires the trigger.Test.stopTest();— marks the end.- (blank line)
Case saved = [SELECT Assigned_Queue__c FROM Case WHERE Id = :c.Id];— re-queries.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.}— closes the test method.}— 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');
}
}
@isTest— marks the class.private class CaseRoutingTest {— the same test class as before.// ... caseWithAMatchingRuleRoutesToTheConfiguredQueue() from above ...— the first test method still lives here too.- (blank line)
@isTest— marks a second test method.static void caseWithNoMatchingRuleFallsBackToGeneralSupport() {— a clearly-named test for the fallback.Test.startTest();— marks the start.Case c = new Case(Subject = 'Unusual request', Priority = 'Low', Type = 'Feature Request');— choosing aPriority/Typecombination deliberately unlikely to have a configured rule.insert c;— fires the trigger.Test.stopTest();— marks the end.- (blank line)
Case saved = [SELECT Assigned_Queue__c FROM Case WHERE Id = :c.Id];— re-queries.System.assertEquals('General Support', saved.Assigned_Queue__c, 'Expected the fallback queue when no rule matches');— confirms Lesson 4's fallback logic actually fires.}— closes the test method.}— 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);
}
}
}
@isTest— marks the class.private class CaseRoutingTest {— the same test class.// ... previous test methods ...— the earlier two tests still live here too.- (blank line)
@isTest— marks a third test method.static void bulkInsertWithMixedPriorityAndTypeRoutesEachCaseCorrectly() {— a clearly-named test for the batch scenario.List<Case> batch = new List<Case>{— three Cases with different Priority/Type combinations.new Case(Subject = 'A', Priority = 'High', Type = 'Problem'),— the first.new Case(Subject = 'B', Priority = 'Low', Type = 'Question'),— the second.new Case(Subject = 'C', Priority = 'Medium', Type = 'Problem')— the third.};— closes the list literal.- (blank line)
Test.startTest();— marks the start.insert batch;— inserts all three together.Test.stopTest();— marks the end.- (blank line)
List<Case> saved = [SELECT Subject, Assigned_Queue__c FROM Case WHERE Id IN :batch];— re-queries all three.for (Case c : saved) {— loops over the results.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.}— closes the loop.}— closes the test method.}— 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.
Test the Feature Quiz
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.