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
jayesh pagarejayesh pagare 

Need Help for code coverage on test class????????????

below is the trigger. Bold lines not covered in test class. (code coverage is 77%) 

trigger updateAccOwner on Account (after update) {
    List<Id> ownerChangedAccountlist = new List<Id>();
    Map<Id,Id> accountownerMap = new Map<Id,Id>();
    List<custObj1__c> lstCust1 =  new List<custObj1__c>();
    List<custObj2__c> lstCust2 = new List<custObj2__c>();
  
    for(Account acc:Trigger.new)
    {
       if(acc.ownerId!=Trigger.oldmap.get(acc.Id).ownerid)
       {          
          ownerChangedAccountlist.add(acc.Id);
          accountownerMap.put(acc.Id,acc.ownerId);
       } 
    }
   list<custObj1__c> custList = [select id,ownerid,Account__c  from custObj1__c where Account__c in:ownerChangedAccountlist];
  for(custObj1__c acct:custList)
   {        
      acct.ownerid = accountownerMap.get(acct.Account__c);
      lstCust1.add(acct); 

   }    
     update lstCust1;   
           List<custObj2__c> accList = [select id,ownerid,Account__c from custObj2__c where Account__c in:ownerChangedAccountlist];
              for(custObj2__c acc : accList)
            {
               acc.OwnerId = accountownerMap.get(acc.Account__c);
               lstCust2.add(acc);

              }
        update lstCust2;
           }


Test class :

     Static testmethod void testupdateAccOwner()
     {   
                       
         Account a = new Account();
        a.Name = 'Test Account';             
        insert a;                
           
       custObj1__c acct = new custObj1__c(Account_Plan_Name__c = 'Test Account Plan'); 
        acct.Account__c = a.Id; 
        acct.OwnerId = a.OwnerId;
        insert acct; 
         
        custObj2__c acc = new custObj2__c();
        acc.Account_acc_Name__c = 'Test account acc';
        acc.custObj1__c = acct.Id;            
        acc.Account__c = a.Id;      
        insert acc;
        
        a.OwnerId = [select id from user limit 1].id;
        update a;
        
        Test.startTest();
            
            acct.OwnerId = a.OwnerId;
             update acct;
            acc.ownerId = acct.OwnerId;
            update acc;
         
        Test.stopTest();  
     }
 
olegforceolegforce
Do you see any exceptions in the debug log and does custList and accList return any recods in test (add system.debug there) ?
jayesh pagarejayesh pagare
yes i have checked in debug log and it return NULL for custList and accList
olegforceolegforce
You need to insert records before running tests in @testSetup method.