+ Start a Discussion
shravani milshravani mil 

how to write test class for @future class

Hi


  @future
    public static void addLeadShares(Set<String> leadUserIds){
      List<ErrorLog__c> errLogs = new List<ErrorLog__c>();
      List<LeadShare> leadShares = new List<LeadShare>();
      for(String leadUsrStr : leadUserIds){
        
        String leadId = leadUsrStr.split('_')[0];
        String userId = leadUsrStr.split('_')[1];
        LeadShare ld = new LeadShare(LeadId = leadId, UserOrGroupId = userId, LeadAccessLevel = 'Read');
        leadShares.add(ld);
        
      }
      Database.SaveResult[] inRes = Database.insert(leadShares);
      // fetch LeadShare Id, then fetch LeadId from Map and add error to that lead, to stop the lead 
    // from further processing.
      for(Integer i=0;i<inRes.size();i++){
      if(!inRes[i].isSuccess()){
        errLogs.add(Utils.getErrorLogObj('Class : LeadTAHelper', 'LeadTAHelper : Error in LeadShare Insertion' + inRes[i].getErrors()[0].getMessage(), 'High'));
      }
    }
    insert errLogs;
    }

Thanks.
Himanshu MaheshwariHimanshu Maheshwari
Hi

Testing future methods is a little different than typical Apex testing. To test future methods, enclose your test code between the startTest and stopTest test methods. The system collects all asynchronous calls made after the startTest. When stopTest is executed, all these collected asynchronous processes are then run synchronously. You can then assert that the asynchronous call operated properly.
 
Test.startTest();
      className.addLeadShares(Set<String>);
Test.stopTest();

Thanks,
Himanshu