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
Shikha ShetShikha Shet 

Trigger test class fails - trigger updates correctly

Hi,
 
I am having a custom object Employee__c, where I have written a trigger to update a custom field Employee_Name__c on change of owner field in Employee__c. The trigger works as expected. But still my test class fails saying as Expected 'first last', Actual 'null', though code is running as desired. Below is my code. Can anyone tell me why my test class is failing?

trigger UpdateNameOnOwnerChange on Employee__c (before update) 
{
    Map<Id,String> UserMap = new Map<id,String>();
    List<User> uList = [select Id, Name from User];
    for(User u : uList)
    {
        UserMap.put(u.Id, u.Name);
    }
   // List<Employee__c> empList = new List<Employee__c>();
    for(Employee__c emp : trigger.new)
    {
        Employee__c empold = trigger.oldmap.get(emp.Id);
        String oldempOwn = empold.OwnerId;
        String newempOwn = emp.OwnerId;
        system.debug('OwnerId new '+newempOwn+' old owner Id '+oldempOwn );
        if(newempown != oldempOwn)
        {
            //system.debug('emp owner name'+emp.Owner.Username);
            if(UserMap.containskey(emp.OwnerId))
            {                                
               // emp.Employee_Name__c= usermap.get(emp.OwnerId).name;
                system.debug('map val'+usermap.get(emp.ownerid));
                emp.Employee_Name__c = usermap.get(emp.ownerid);
                //empList.add(emp);
                system.debug('Name is '+emp.Employee_Name__c);
            }            
        }
    }

Below is my test class for the same.

@istest
public class TestUpdateOwnerEmp 
{
  @testsetup
    static void setup()
    {
        //UserRole r = new UserRole(DeveloperName = 'MyCustomRole', Name = 'My Role');
        //insert r;
        User u =new User(
        ProfileId = [select id from profile where name = 'Std Cont Cust Obj'].Id,
           FirstName = 'first0',                
               LastName = 'last0',
               Email = 'puser000@gmail.com',
               UserName = 'puser000@gmail.com',
               CompanyName = 'Test',
               Title = 'title',
               Alias = 'alias',
               TimeZoneSidKey = 'America/Los_Angeles',
               EmailEncodingKey = 'UTF-8',
               LanguageLocalekey = 'en_US',
               LocaleSidKey ='en_US');
               //UserRole = r.Id);
    insert u;
        User u1 =new User(
        ProfileId = [select id from profile where name = 'Std Cont Cust Obj'].Id,
               FirstName = 'first',
                 LastName = 'last',
               Email = 'puser001@gmail.com',
               UserName = 'puser001@gmail.com',
               CompanyName = 'Test',
               Title = 'title',
               Alias = 'alias',
               TimeZoneSidKey = 'America/Los_Angeles',
               EmailEncodingKey = 'UTF-8',
               LanguageLocalekey = 'en_US',
               LocaleSidKey ='en_US');
        insert u1;
        
        Account a = new Account(Name = 'Test');
        insert a;
        
        Contact c = new Contact(Firstname = 'First', Lastname = 'Last', email = 'firstlast@test.com', accountId = a.id);
        insert c;
        
        Employee__c emp = new Employee__c();
        //emp.AccountId__c = '0012800000tbM5E';
        emp.ContactId__c = c.Id;
        emp.Employee_Name__c = 'test';
        emp.Salary__c = 12200;
        emp.OwnerId = u.Id;
        insert emp;
        
    }
    
    static testmethod void testownerchange()
    {
        test.startTest();
        user u = [select Id, name from User where UserName = 'puser001@gmail.com'];
        //User u = [select id,Name from User];
        Employee__c emp = [select Id,OwnerId, Employee_Name__c from Employee__c];
        system.debug('Employee '+emp);
        emp.OwnerId = u.Id;        
        
        Database.SaveResult res = Database.update(emp,false);
        system.debug('success status '+res.isSuccess());
        //system.assertEquals(u.Name, emp.Employee_Name__c);
        
        test.stopTest();
        system.assertEquals(u.Id, emp.OwnerId);
        system.assertEquals(u.name, emp.Owner.name);
        system.assertEquals(emp.Owner.Name, emp.Employee_Name__c);
        system.debug('user name'+u.name);
        system.debug('emp name'+emp.Employee_Name__c);        
        
    }
}
GhanshyamChoudhariGhanshyamChoudhari
After insertion of an employee record, you update the same record.
 
Employee__c ec = [select id from Employee__c where id=emp.id];
 ec.Salary__c = 1222222;
update ec;


 
Shikha ShetShikha Shet

Hi Ghanshyam,

yes, I am updating owner of the Employee record which was inserted. Employee record is already inserted in testsetup. And I am trying to update that record in my testmethod.
 Employee__c emp = [select Id,OwnerId, Employee_Name__c from Employee__c];       
 emp.OwnerId = u.Id;        

Thanks,
Shikha