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
OssieOssie 

Help with writing a Test Class for basic Trigger

Hi All,

I have written a basic trigger and currently have code coverage of 66%.

Not sure how to cover lines 8 and 9. Could anyone help me please??

trigger CaseAssignmentOutofOffice on Case (before update) {
    
    For (Case c : Trigger.new) {
        Case NewCase = Trigger.newMap.get(c.id);
        Case OldCase = Trigger.oldMap.get(c.id);

        If (OldCase.OwnerId != NewCase.OwnerId && c.Status != 'Closed' && NewCase.Back_Up__c != Null){
        c.OwnerId = NewCase.Back_Up__c;
        c.IsBackUp__c = True;
    }
}
Best Answer chosen by Ossie
Himanshu ParasharHimanshu Parashar
Hi Ossie,

Try following test method.
 
static testMethod void  TestCaseMethod(){

//Insert user
        Userlist=new List<user>();
        Profile pf = [Select id from Profile where  name = 'System Administrator' LIMIT 1];
        
        for(integer i=0;i<=8;i++)
        {
         user usr = new User();
         usr.firstName = 'test12' + i;
         usr.LastName = 'test22'+ i;
         usr.Alias = '122'+ i;
         usr.Email = 'xy@demo.com + i;
         usr.UserName='testuser2@testclass.com'+ i;
         usr.ProfileId = pf.id;
         usr.CommunityNickname = usr.firstname+'_'+usr.lastName;
         usr.EmailEncodingKey ='ISO-8859-1';
         usr.LanguageLocaleKey = 'en_US';
         usr.TimeZoneSidKey ='America/New_York';
         usr.LocaleSidKey = 'en_US';
         Userlist.add(usr);
        }
        
        insert Userlist;

Case cs = new Case(ownerid=userlist[0].id,status='Open',Back_Up__c=userlist[0].id);
insert cs;

Case cs1 = [select id,ownerid from case where id=: cs.id];
cs1.ownerid=userlist[1].id;

update cs1;



}

Thanks,
Himanshu

All Answers

Himanshu ParasharHimanshu Parashar
Hi Ossie,

Try following test method.
 
static testMethod void  TestCaseMethod(){

//Insert user
        Userlist=new List<user>();
        Profile pf = [Select id from Profile where  name = 'System Administrator' LIMIT 1];
        
        for(integer i=0;i<=8;i++)
        {
         user usr = new User();
         usr.firstName = 'test12' + i;
         usr.LastName = 'test22'+ i;
         usr.Alias = '122'+ i;
         usr.Email = 'xy@demo.com + i;
         usr.UserName='testuser2@testclass.com'+ i;
         usr.ProfileId = pf.id;
         usr.CommunityNickname = usr.firstname+'_'+usr.lastName;
         usr.EmailEncodingKey ='ISO-8859-1';
         usr.LanguageLocaleKey = 'en_US';
         usr.TimeZoneSidKey ='America/New_York';
         usr.LocaleSidKey = 'en_US';
         Userlist.add(usr);
        }
        
        insert Userlist;

Case cs = new Case(ownerid=userlist[0].id,status='Open',Back_Up__c=userlist[0].id);
insert cs;

Case cs1 = [select id,ownerid from case where id=: cs.id];
cs1.ownerid=userlist[1].id;

update cs1;



}

Thanks,
Himanshu
This was selected as the best answer
Arunkumar RArunkumar R
Hi Ossle,

Find the below code,
 
@isTest
private class CaseTriggerTest
{
    static testMethod void createCase()
    {
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
        User u = new User(Alias = 'standt', Email='testuser823@testorg.com', 
                          EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US', 
                          LocaleSidKey='en_US', ProfileId = p.Id, 
                          TimeZoneSidKey='America/Los_Angeles', UserName='testuser354@testorg.com');
        insert u;
        
        Case c = new Case();
        
        System.runAs(u)
        {
            c.Status = 'New';
            c.Back_Up__c = 'test';
            insert c;
        }
        update c;
    }
    
}

Logic:

1. Insert a new user, then insert case by using the system.runAs(). So the owner will be a new inserted owner.
2. Update a case in the outer of system.runAs(), so the owner will become the current user. 

So your trigger logic will satisfied. Please mark this as a solution, if you are satisfy this solution.
OssieOssie
Thank you all for your help.
Greatly appreciated.