function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
William KeamWilliam Keam 

Help with testing a trigger that calls @future method in a separate APEX class

Hello everyone,

I have hit a wall and need help on how to test @future methods which are called from a trigger.

I think it has something do with the fact that @future methods are no necessarily processed immediately and as such my assertions are not passing, any help on how to get my code covered would be greatly appreciated,

What the trigger does is reassigns open and waiting cases to a queue when a user is disabled.

The trigger:
trigger user_deactivated_reassign_case on User (before update) {
    
    for(User u : trigger.new){
        User oldUsr = Trigger.oldMap.get(u.Id);
        if(oldUsr.IsActive==TRUE && u.IsActive==FALSE){
            User newUsr = Trigger.newMap.get(u.Id);
            string recordIds = u.Id;
            user_deactivation_reassign_case_class.reassign_case(recordIds);
        }
    }
}

The @future Apex Class:
public class user_deactivation_reassign_case_class {
    @future
    public static void reassign_case(string recordIds){
        List<Case> cas = [SELECT Id, Type FROM Case WHERE OwnerId=:recordIds];
        List<Case> casesTobeUpdated = new List<Case>();
               
        for(Case currentCase : cas){
            if(currentCase.Status=='Open'||currentCase.Status=='Waiting Response'||currentCase.Status=='On Hold'){
                if(currentCase.Type=='AU.COM'||currentCase.Type=='Customer Service'){
                    currentCase.OwnerId='00G90000001mq6F'; //Premium Support L2
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Management'){
                    currentCase.OwnerId='00G90000001mq5m'; //Management
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Administration'||currentCase.Type=='Domain Dispute'){
                    currentCase.OwnerId='00G90000001mq65'; //Premium Admin
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='EDU.AU'||currentCase.Type=='GOV.AU'||currentCase.Type=='Corporate'){
                    currentCase.OwnerId='00G90000001mzrl'; //Corporate
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='VPS Unmanaged'||currentCase.Type=='VPS Managed'){
                    currentCase.OwnerId='00G90000001nJfA'; //VPS Unmanaged
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Sales'||currentCase.Type=='Social Media Sales'||currentCase.Type=='Online Marketing Sales'){
                    currentCase.OwnerId='00G90000001mzrH'; //Retail Sales
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Online Marketing Support'){
                    currentCase.OwnerId='00G90000001mzrR'; //Online Marketing Support
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Web Design'){
                    currentCase.OwnerId='00G90000001mzT4'; //Web Design
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Website Security'){
                    currentCase.OwnerId='00G90000001nAl0'; //Website Security
                    casesTobeUpdated.add(currentCase);
                }
                else if(currentCase.Type=='Credit Control'){
                    currentCase.OwnerId='00G90000001nAlK'; //Credit Control
                    casesTobeUpdated.add(currentCase);
                }
                else{
                    currentCase.OwnerId='00G90000001mq6F'; //Premium Support L2
                    casesTobeUpdated.add(currentCase);
                }
            }
        }
    update casesTobeUpdated;
    }
}

And the test class which I can't figure out:
@isTest
public class user_deactivation_reassign_case_test {
    public static testmethod void test_user_termination(){
        User thisUser = [SELECT Id FROM USER WHERE Name='System Automation'];
        
        system.runAs(thisUser){
        User u = new User(FirstName='Test',
                          LastName='Test',
                          Username='apextest@netregistry.com', 
                          Email='apextest@netregistry.com', 
                          Alias='test', 
                          CommunityNickname='test', 
                          TimeZoneSidKey='Australia/Sydney', 
                          LocaleSidKey='en_US', 
                          EmailEncodingKey='UTF-8', 
                          ProfileId='00e90000001NAum', 
                          LanguageLocaleKey='en_US');
        
        insert u;
        Account acc = new Account(Account_Ref__c='1',Name='Test', Reseller_Id__c=1, Virtualisation_Id__c=1);
        insert acc;
        Case cas = new Case (AccountId=acc.Id,OwnerId=u.Id,Type='AU.COM',Status='Open',Brand__c='Netregistry');
        insert cas;
        update u;
        string recordIds = u.Id;

        system.assertEquals(cas.OwnerId, '00G90000001mq6F');
        }
    }
}


I have read other posts saying I need to use Test.startTest(); and Test.stopTest(); but I can't figure out where this fits into a @future class being called via a trigger
kiranmutturukiranmutturu
Test.startTest();

 update u;

Test.stopTest();
BalajiRanganathanBalajiRanganathan
Usually your testmethod should follow this sequence.

1) load your test data
2) Test.startTest()
3) Test code
4) Test.stopTest()
5) Asserts

look into the below link to understand how startTest and stopTest helps.

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_test.htm#apex_System_Test_startTest