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
Kunal Purohit 4Kunal Purohit 4 

How to write test class for below Handler?

Hello Folks, I am trying to write test class for below code. Its not moving beyond 45%. Can you please help me to achieve the same?

public class SumAmountOppHandler {
    
     List<Account> accList=new List<Account>();
     Set<Id> setAccIds = new Set<Id>();
    public  void afterinsert(List<Opportunity> newoppylist){
  for(Opportunity con : newoppylist){
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
               updateamount(setAccIds) ;
                }
                        } 
    }
      public  void afterupdate(List<Opportunity> newoppylist, Map<Id,Opportunity> mapoppy){
    
        for(Opportunity con : newoppylist){ 
            if(con.AccountId!=mapoppy.get(con.Id).AccountId && con.Amount>10000.00){
                setAccIds.add(con.AccountId);
                setAccIds.add(mapoppy.get(con.Id).AccountId);
                updateamount(setAccIds) ;
                }
            
                        }        
        
    }
      public  void afterdelete(List<Opportunity> oldoppylist){
  for(Opportunity con : oldoppylist){
            if(con.AccountId != null && con.Amount>10000.00){
            setAccIds.add(con.AccountId);
                updateamount(setAccIds) ;
                }
                        } 
    }
    
    public void updateamount(set<id> accids){
          for(Account acc :[Select id,Total_Opty_Amount__c  ,(Select Amount from Opportunities where Amount>10000.00) from Account where Id in : accids]){
                        
        acc.Total_Opty_Amount__c  = acc.Opportunities.size();
        acclist.add(acc);
        
    }
    if(acclist.size()>0){
        update accList;     
    }
    }

}
Best Answer chosen by Kunal Purohit 4
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

The test class can be as below.
 
@istest
public class SumAmountOppHandlerTest {
     @istest
    static void OppCount()
    {
      Account acc= new Account();
        acc.name= 'sample account';
        acc.ISSN_Number__c = 111;
        insert acc;
        
        Account acc1 = new Account();
        acc1.Name = 'test account';
       acc1.ISSN_Number__c = 2222;
        insert acc1;
        
           Opportunity opp = new Opportunity();
        opp.AccountId = acc.Id;
        opp.Amount=15000.00;
        opp.Name = 'Test sum';
        opp.StageName = 'Test picklist';
        opp.CloseDate= date.newInstance(2023, 11, 10);
        insert opp;
        
        
        Opportunity Opp1 = new Opportunity();
        opp1.AccountId = acc1.Id;
        opp1.Amount=15000.00;
        opp1.Name = 'Test sum2';
        opp1.StageName = 'Test picklist2';
        opp1.CloseDate= date.newInstance(2023, 11, 10);
        insert opp1; 
        
        opp1.accountid=acc.id;
        update opp1;
        delete opp1;
    }

}

It will cover both Trigger and handler 100%.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,