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
sumit dsumit d 

test class for opportuntyTriggerHelper

Hi All,
my opportunityTriggerHelper is given below:-
public without sharing class OpportunityTriggerHelper  {
    
    public static List<Opportunity> newOpportunity = new List<Opportunity>();
    public static List<Opportunity> oldOpportunity = new List<Opportunity>();
    public static Map<Id, Opportunity> newMapOpportunity = new Map<Id, Opportunity>();
    public static Map<Id, Opportunity> oldMapOpportunity = new Map<Id, Opportunity>();
    
    public static boolean runTrigger = TRUE;
     public static void populateArrDifference(){
        list<Opportunity> releventOpportunities = new list<Opportunity>();
        for( Opportunity opp : newOpportunity ){
            if( opp.Type == 'Renewal Expansion' && 
              (  trigger.isInsert || ( opp.Type != oldMapOpportunity.get( opp.Id ).Type ))){
                releventOpportunities.add( opp );
            }
        }
        try{
            set<String> oppAcceptedTypes = new set<String>
                                            { 'New Business', 'Renewal', 
                                              'Winback', 'AE Winback (>120 days post exp)' 
                                             };
            set<Id> accountIds = new set<Id>();
            if( releventOpportunities.size() > 0 ){
                for( Opportunity opp : releventOpportunities ){
                    accountIds.add( opp.AccountId );
                }
            }
            
            List<Opportunity> opportunities =  [ SELECT Id, AccountId, Name,
                                                Description, StageName,
                                                ARR_Estimation__c
                                                FROM Opportunity 
                                                WHERE AccountId IN: accountIds
                                                AND StageName = 'Closed Won'
                                                AND Type IN :oppAcceptedTypes
                                                ORDER BY CreatedDate DESC 
                                               ];
            
            map<Id, Opportunity> mapAccountIdToArrEstimation = new map<Id, Opportunity>();
            if( opportunities.size() > 0 ){
                for( Opportunity opp : opportunities ){
                    if( ! mapAccountIdToArrEstimation.containsKey( opp.AccountId ) ){
                        mapAccountIdToArrEstimation.put( opp.AccountId, opp );
                    }
                }
            }
            
            list<Opportunity> opportunityToUpdate = new list<Opportunity>();
            for( Opportunity opp : releventOpportunities ){
                if( mapAccountIdToArrEstimation.containsKey( opp.AccountId ) ){
                    opp.Recent_Closed_Won_Opportunity__c = mapAccountIdToArrEstimation.get( opp.AccountId ).Id;
                }
            }
            
        }
        catch( Exception e ){
            releventOpportunities.get(0).addError( e.getMessage() );
        }
        
    }
    }
 how to write test class for it?
 Any suggestions?
Martha VMartha V
For the test class you insert a list  of opportunities, then you update the opportunities you inserted to test the update trigger. The inserted opportunities and the updated fields should test true and false (some true, some false) to be able to run in every line of your Trigger Class.

for example for the followign if
if( opp.Type == 'Renewal Expansion' && 
              (  trigger.isInsert || ( opp.Type != oldMapOpportunity.get( opp.Id ).Type )))
you should insert an opportunity with the type "Renewal Expansion" or update an opportunity and change the Type.