+ Start a Discussion
OssieOssie 

Apex Test Class - Help Plz

Hi All,

I need help please with my Test Class.
I have 60% code coverage. See below. Any help would be great. Thanks! 
 
trigger ManageUserCase on Case (before update){
    set<String> usrIds = new set<String>();    
    
    for(Case cs :Trigger.New){
        if(Trigger.isInsert || (Trigger.isUpdate && (cs.OwnerId != Trigger.OldMap.get(cs.Id).OwnerId))){
            usrIds.add(cs.OwnerId);
        }
    }
      
    if(usrIds.size() > 0){
        map<string,User> userMap = new map<string,user>();        
        for(User u : [select Id, Back_Up__c, Back_Up__r.Name, User_Out__c from User Where Id IN: usrIds 
                      and User_Out__c = true]){
            userMap.put(u.Id,u);
        }        
        
        for(Case cs : Trigger.new){
            if(userMap.containsKey(cs.OwnerId)){
                string otherUsername = userMap.get(cs.OwnerId).Back_Up__r.Name;
                if(otherUsername == null)
                    otherUsername = 'Someone else';
                    cs.addError('Please assign Case to '+otherUsername);
            }
        }
    }
}

Test Class:
 
@isTest

private class TestManageUserCase {

public static testMethod void TestManageUserCase(){

//Insert user
        List<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';
         usr.User_Out__c = False;
         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, back_up__c from case where id=: cs.id];
cs1.ownerid=userlist[1].id;

update cs1;

}
}

 
Nitin PaliwalNitin Paliwal
Hi,
Please try below code,

@isTest

private class TestManageUserCase {

public static testMethod void TestManageUserCase(){

//Insert user
        List<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';
         usr.User_Out__c = true;
         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, back_up__c from case where id=: cs.id];
cs1.ownerid=userlist[1].id;

update cs1;

}
}

Thanks
Nitin
OssieOssie
Hi Nitin,

I have made the change but my test class is now failing.
I get error message "System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, The user out is ticked, therefore please select a Back Up.: [Back_Up__c]"
Nitin PaliwalNitin Paliwal
HI Ossie,
Its a validation on your user.Can you fill the "Back_Up__c" field in the test class code where the user are being created.

Thanks
Nitin
OssieOssie
Hi Nitin,

Done - but now getting error message "System.DmlException: Update failed. First exception on row 0 with id 500c00000059XBYAA2; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, user out assign case to System Administrator: []"

it doens like line 39 update cs1; 

I dont understand this should work!!! Any ideas??
@isTest

private class TestManageUserCase {

public static testMethod void TestManageUserCase(){

//Insert user
        List<User> Userlist = new List<user>();
        Profile pf = [Select id from Profile where  name = 'System Administrator' LIMIT 1];           User u = [Select id from User where name LIKE '%System%']; 
                   
        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';
         usr.User_Out__c = True;
         usr.Back_Up__c = u.id;
         Userlist.add(usr);
        }
        
        insert Userlist;

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

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

    update cs1;

}
}

 
Nitin PaliwalNitin Paliwal
HI,
Try commenting the "update cs1" line.
It wont make any effect on the coverage.Your class would be covered.


Thanks
Nitin
OssieOssie
Hi Nitin,

I commented the update cs1; line and the code coverage dropped to 26%.
This is very strange!!
Nitin PaliwalNitin Paliwal
HI Ossie,
Your trigger is not running on before insert, whereas in the trigger code you are checking the condition for the Insert.

What is the exact requirement to be running or insert or not?

trigger ManageUserCase on Case (before update)

Thanks
Nitin

 
OssieOssie
Hi Nitin,

It should only run on <before update>.

I have removed any reference to trigger.isInsert. Re-ran the test class and commented out update cs1; and code coverage is still 26%.  
 
trigger ManageUserCase on Case (before update){
    set<String> usrIds = new set<String>();    
    
    for(Case cs :Trigger.New){
        if(Trigger.isUpdate && (cs.OwnerId != Trigger.OldMap.get(cs.Id).OwnerId)){
            usrIds.add(cs.OwnerId);
        }
    }
      
    if(usrIds.size() > 0){
        map<string,User> userMap = new map<string,user>();        
        for(User u : [select Id,Back_Up__c,Back_Up__r.Name, User_Out__c from User Where Id IN: usrIds 
                      and User_Out__c = true]){
            userMap.put(u.Id,u);
        }        
        
         for(Case cs : Trigger.new){
            if(userMap.containsKey(cs.OwnerId)){
                string otherUsername = userMap.get(cs.OwnerId).Back_Up__r.Name;
                if(otherUsername == null)
                    otherUsername = 'Someone else';                
                cs.addError('This user is out of the office, therefore please assign query to '+otherUsername);
            }
        }
    }
}

 
Nitin PaliwalNitin Paliwal
Hi,
Now you have to uncomment the "update cs1" line as the trigger is on Update only.
I think you have multiple validation, which are contradicting one another.Please try and solve those first.As can be seen both the validation are for the OWNERS who have USER_OUT set as true.

Thanks
Nitin
 
OssieOssie
Hi,

My validation rules are working correctly. They are validating whether a Back_Up__c is entered when User_Out__c has been selected and vice versa.
The Update cs1 is failing because when you change the Case ownerId then an error message would be displayed to the user asking them to assign Case to Back_Up__c becuase User_Out__c is set to true. Unfortunatlyy i do not know how to test the error message / re-assigning to back_up__c.