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
Nilima KumariNilima Kumari 

Need Help to to write Test Class for the Apex class

Hello Developers

i am trying to design a Test class for the Apex class but i am not able to do the code coverage of the following apex class, can anyone please help
 
public class FutureHandler {
    
    @future
    public static void updaterecs(Map<String,Id> oldManagerQuotaMap1){
        Map<Id,Quota__c> oldMapQuota = new Map<Id,Quota__c>();
        if(null != oldManagerQuotaMap1 && oldManagerQuotaMap1.size() > 0){
            for (aggregateResult result: [Select Manager_Quota__c, Sum(Inside_Sales_Roll_Up__c) 
                                          FROM Quota__c WHERE 
                                          Manager_Quota__c IN: oldManagerQuotaMap1.values() 
                                          GROUP BY Manager_Quota__c]) {
                                              system.debug('result'+result)    ;          
                                              oldMapQuota.put((Id)result.get('Manager_Quota__c'),new Quota__c(Id=(Id)result.get('Manager_Quota__c'),Actual_Amount__c =(Decimal)result.get('expr0')));         
                                          }
            
            for(Id qId : oldManagerQuotaMap1.values()){
                if(!oldMapQuota.containskey(qId)){
                    oldMapQuota.put(qId, new Quota__c(Id = qId,Actual_Amount__c = 0.0));
                }
            }
            
            if(null != oldMapQuota && oldMapQuota.size() > 0){
                update oldMapQuota.values();
            }
            
        }
    }

    public static void sendOppsForApproval(List<Opportunity> opps){
        
        List<Opportunity> oppsToBeSentForApproval = new List<Opportunity>();
        String monthYear = System.now().format('MMMM-yyyy');
        String month = monthYear.split('-')[0];
        String year = monthYear.split('-')[1];
        Date nextMonthStart = System.today().toStartOfMonth().addMonths(1).toStartOfMonth();
        List<Aggregate_Approval__c> appr = [Select Id from Aggregate_Approval__c where Month__c=:month And Year__c=:year]; 
        Id approvalId;
        if(appr.size()>0){
            approvalId = appr[0].Id;
            
            for(Opportunity opp : opps){
                opp.Aggregate_Approval__c = approvalId;
                opp.Status__c = 'In Approval';
                oppsToBeSentForApproval.add(opp);
            }
            update oppsToBeSentForApproval;
           // List<Approval.LockResult> li = Approval.lock(opps);
            
            Approval.ProcessSubmitRequest oppAggregateApproval = new Approval.ProcessSubmitRequest();
            oppAggregateApproval.setComments('Submitting opportunities for Approval for '+System.now().format('MMMM-yyyy'));
            oppAggregateApproval.setObjectId(approvalId);
            Approval.ProcessResult approvalResult = Approval.process(oppAggregateApproval); 
            
        } 
    }
}
My Test Class
 
@isTest
public class FutureHandlerTestclass {
    
    static testmethod void Futuretest(){
        
        List<Quota__c> Ftur = new list <Quota__c>();
        Quota__c FF = new Quota__c();
        FF.Name='Julian';
        FF.Assigned_To__c = userinfo.getUserId();
        FF.Month__c='March';  
        FF.Quater__c='Q1';       
        FF.Quater_Year__c = '2020';
        FF.Actual_Amount__c = 100;
        FF.Unique_Identifier_Per_Quater_Per_User__c = 'Hello';
        Ftur.add(FF);
        insert FF;
        
        
        Quota__c FF1 = new Quota__c();
        FF1.Name='rahul';
        FF1.Assigned_To__c = userinfo.getUserId();
        FF1.Month__c='October';  
        FF1.Quater__c='Q4';       
        FF1.Quater_Year__c = '2030';
        FF.Actual_Amount__c = 200;
        FF1.Unique_Identifier_Per_Quater_Per_User__c = 'Helloworld';
        Ftur.add(FF1);
        insert FF1;
        
        Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        
        User u = [Select id,name from user where isactive=true Limit 1];
        
        List<Opportunity> Oppps = new list <Opportunity>(); 
        Opportunity oop = new Opportunity();
        
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.Billing_To_Be_Initiated__c = 'Yes';
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';        
        Oppps.add(oop);
        insert oop;
    }
    }

 
Rushita Bavishi 1Rushita Bavishi 1

Hi Nilima,

If it is  just a class(Not called from trigger) ,Then First you need to insert class mathod at the End of method. Otherwise it will not call class. I have updated your test class. Hope it will help you.

@isTest
public class FutureHandlerTestclass {
    
    static testmethod void Futuretest(){
        
		Map < String, Id> QuotaMap = new Map<String , Id>(); 
		
        Quota__c FF = new Quota__c();
        FF.Name='Julian';
        FF.Assigned_To__c = userinfo.getUserId();
        FF.Month__c='March';  
        FF.Quater__c='Q1';       
        FF.Quater_Year__c = '2020';
        FF.Actual_Amount__c = 100;
        FF.Unique_Identifier_Per_Quater_Per_User__c = 'Hello';
        QuotaMap.Put('1', FF.Id);
        
        Quota__c FF1 = new Quota__c();
        FF1.Name='rahul';
        FF1.Assigned_To__c = userinfo.getUserId();
        FF1.Month__c='October';  
        FF1.Quater__c='Q4';       
        FF1.Quater_Year__c = '2030';
        FF.Actual_Amount__c = 200;
        FF1.Unique_Identifier_Per_Quater_Per_User__c = 'Helloworld';
        Ftur.add(FF1);
        //insert FF1;
        QuotaMap.Put('2', FF1.Id);
		insert QuotaMap;
        
		Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        
        User u = [Select id,name from user where isactive=true Limit 1];
        
        List<Opportunity> Oppps = new list <Opportunity>(); 
        Opportunity oop = new Opportunity();
        
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.Billing_To_Be_Initiated__c = 'Yes';
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';        
        Oppps.add(oop);
        insert oop;
		
		FutureHandler.updaterecs(QuotaMap);
		FutureHandler.sendOppsForApproval(Oppps);
    }
    }
Nilima KumariNilima Kumari
@Rushita

its throwing an error in the line 29, says DML requires SObject or SObject list type: Map<String,Id>, how do i resolve it


 
@isTest
public class FutureHandlerTestclass {
    
    static testmethod void Futuretest(){
        
        Profile pf= [Select Id from profile where Name='System Administrator'];  
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-',''); 
        
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        
        
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id,
                         ManagerId= UserInfo.getUserId()); 
        Insert uu;
        
		Map <String, Id> QuotaMap = new Map<String , Id>(); 
        List<Quota__c> Ftur = new list <Quota__c>();
		
        Quota__c FF = new Quota__c();
        FF.Name='Julian';
        FF.Assigned_To__c = uu.Id;
        FF.Month__c='March';  
        FF.Quater__c='Q1';       
        FF.Quater_Year__c = '2020';
        FF.Actual_Amount__c = 100;
        FF.Unique_Identifier_Per_Quater_Per_User__c = 'Hello';
        QuotaMap.Put('1', FF.Id);
        
        Quota__c FF1 = new Quota__c();
        FF1.Name='rahul';
        FF1.Assigned_To__c = uu.Id;
        FF1.Month__c='October';  
        FF1.Quater__c='Q4';       
        FF1.Quater_Year__c = '2030';
        FF.Actual_Amount__c = 200;
        FF1.Unique_Identifier_Per_Quater_Per_User__c = 'Helloworld';
        Ftur.add(FF1);
        //insert FF1;
        QuotaMap.Put('2', FF1.Id);
		insert QuotaMap;
        
		Account testAcct = new Account (Name = 'My Test Account');
        insert testAcct;
        
        User u = [Select id,name from user where isactive=true Limit 1];
        
        List<Opportunity> Oppps = new list <Opportunity>(); 
        Opportunity oop = new Opportunity();
        
        oop.Name = 'Test1';
        oop.AccountId = testAcct.ID;
        oop.StageName = 'Demo';
        oop.CloseDate = system.today();
        oop.Billing_To_Be_Initiated__c = 'Yes';
        oop.LeadSource = 'Customer';
        oop.Country__c = 'India';        
        Oppps.add(oop);
        insert oop;
		
		FutureHandler.updaterecs(QuotaMap);
		FutureHandler.sendOppsForApproval(Oppps);
    }
    }