+ Start a Discussion
Febe NagyFebe Nagy 

issues with test class on account owner change

Can anyone help please, I think I'm just getting lost  and going in circles with this test class.  No matter how I compare the new value with the old I still can't get the test to cover the comparison part in my trigger.  Please help me fix this.
*****************************************************************************************************
@isTest
private class testAccOwner {
    public static testMethod void testAccOwnerChange() 
    {
    test.startTest();

      	List<User> usrList = new List<User>();
        User testUser2 = [Select Id, ManagerId from User where IsActive = true limit 1];
        User testUser3 = [Select Id, ManagerId from User where IsActive = true limit 1];
         
    	User testUser1 = new User(alias = 'TstUsr1',Firstname='tst1', email='newuserDP@testorg.com', 
                                  emailencodingkey='UTF-8', lastname='Tst11', languagelocalekey='en_US', 
                                  localesidkey='en_US', profileid = 'idNumer04', timezonesidkey='America/Los_Angeles', 
                                  username='newuser11@testorg.com', UserRoleId = 'idNumer03',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = testUser3.Id);
        
        User testUser4 = new User(alias = 'TstUsr2',Firstname='tst2', email='newuserDP2@testorg.com', 
                                  emailencodingkey='UTF-8', lastname='Tst112', languagelocalekey='en_US', 
                                  localesidkey='en_US', profileid = 'idNumer04', timezonesidkey='America/Los_Angeles', 
                                  username='newuser112@testorg.com', UserRoleId = 'idNumer03',Country_picklist__c = 'UNITED STATES',
        Global_Region__c  = 'US', DIRECTOR_NM__c=testUser2.id, ManagerId = 'idNumer02');
        //insert testUser1;
        usrList.add(testUser1);
        usrList.add(testUser4);
        insert usrList;
       
        System.runAs(testUser1){
            
            Account acc = new Account(Name = 'Febe01', 
                                      OwnerId = 'idNumer01',
                                      Previous_Account_Owner__c = 'idNumer01');
            insert acc;
            system.assert(acc.Previous_Account_Owner__c == acc.OwnerId);
                       
            acc.OwnerId = testUser1.Id;
            acc.Previous_Account_Owner__c = 'idNumer01';
            update acc;
            //system.assertEquals('idNumer01', acc.Previous_Account_Owner__c);
            system.assert(acc.Previous_Account_Owner__c != acc.OwnerId);
            
            system.debug('New account owner :  ' + acc.OwnerId + 'Old Account owner: ' + acc.Previous_Account_Owner__c);
            
            
        }
        
       List<Account> lstNewAcc = [select id, name, ownerId from Account where ownerId =:testUser2.Id];
       List<Account> lstOldAcc = [select id, name, ownerId from Account where ownerId =:testUser1.Id];
       map<Id, Account> oldmap = new map<Id, Account>();
       map<Id, Account> newmap = new map<Id, Account>();
            
            for(Account oldAccts : lstOldAcc)
            {
                oldmap.put(oldAccts.Id, oldAccts); 
            }
            
            for(Account newAct : lstNewAcc)
            {
                Account oldAc = oldmap.get(newAct.Id);
                if(newAct.OwnerId != oldAc.OwnerId){
                    newAct.Previous_Account_Owner__c = oldAc.OwnerId;
                    system.assert(newAct.OwnerId != oldAc.OwnerId);
                   	newmap.put(newAct.Id, newAct); 
                }
                
            }
            
            system.debug('***********************************  Account oldmap: '+ oldmap);
         test.stopTest();
    }
}

***************************************************************************************************
This is the trigger: 
trigger AccOwnerChange on Account (before update) {
        
        if(INTLStaticObjectHelper.isAccOwnerChangeExecuted){
            return;
        }
        INTLStaticObjectHelper.isAccOwnerChangeExecuted = true;
        Set<Id> setUserIds = new Set<Id>();
        Map<Id, User> mapUserObj = new Map<Id, User>();
        
        
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                setUserIds.add(a.OwnerId);
                setUserIds.add(oldObj.ownerId);
            }
                
        }
        system.debug('test ###' + setUserIds);
        if(setUserIds.size()>0){
            List<User> lstUser = [select Id, Name, ManagerId from User where Id in :setUserIds AND isActive = true];
            for(User usrObj : lstUser){
                mapUserObj.put(usrObj.Id, usrObj);
            }
        }
        system.debug('test ###' + mapUserObj);
        for(Account a : Trigger.new)
        {
            Account oldObj = Trigger.oldmap.get(a.id);
            if(a.ownerId != oldObj.ownerId){
                a.Previous_Account_Owner__c = oldObj.ownerId;
                if(mapUserObj.get(a.Previous_Account_Owner__c)!=null){
                    a.Previous_Account_Owner_s_Manager__c = mapUserObj.get(a.Previous_Account_Owner__c).ManagerId;
                }
                if(mapUserObj.get(a.ownerId) != null){
                    a.New_Account_Owner_s_Manager__c = mapUserObj.get(a.ownerId).ManagerId;
                }
            }
        }
Keshab AcharyaKeshab Acharya
Trigger will run before update and you are updating the account only once in line # 38. Dont you think you should update it again around line # 67?
Febe NagyFebe Nagy
i'm not sure if this is what you meant, since I couldn't use the same line # 38 around line # 67,  I just picked to update the list of new accts in that section of the code, like below, but it didn't change the coverage of 58%, please help me understand what is wrong?  much appreciate it.
  
            }
            update lstNewAcc;
            system.debug('***********************************  Account oldmap: '+ oldmap);
             test.stopTest();
    }
}