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
Srinivas TestingSrinivas Testing 

Can you write test class below code

Hi Team, 
I am new resource i don't know how to write test class i have written triggerhandler class on opportunity and i post the code below , can you make test class 
public class OpportunityTriggerHandler{
    public void handleTrigger (Opportunity[] trigNew){
        if (trigger.isBefore){
            if(trigger.isUpdate){
                handleBeforeInsert(trigNew);
                }
               } 
            else if (trigger.isInsert){
                handleBeforeInsert(trigNew);
                }
       }         
    public void handleBeforeInsert(Opportunity[] trigNew)
    {
        for (Opportunity objOpps : trigNew)
        {
            if ((objOpps.StageName == 'Prospecting') || (objOpps.StageName == 'Perception Analysis') || (objOpps.StageName == 'Qualification') || (objOpps.StageName == 'Needs Analysis')
                                                 || (objOpps.StageName == 'Value Proposition')||(objOpps.StageName == 'Id.Decision Markers')||(objOpps.StageName == 'Proposal/Price Quote'))
            {
               
               if ( objOpps.Win_Lose_Comment__c != null){   
                objOpps.Win_Lose_Comment__c.addError('Donfill the  value Win/Lose Comment');
                }
                
               if ( objOpps.Win_Lose_Reason__c != null){
                objOpps.Win_Lose_Reason__c.addError('Don t fill  the value Win/Lose Reason');
                }
                
                if( objOpps.Type_Lose__c != null)
                {
                objOpps.Type_Lose__c.addError('Dont fill the value Type Lose');
                }
        
            }
        
            else if ((objOpps.StageName == 'Negotiation/Review') || (objOpps.StageName == 'Closed Won'))
            {
                if ( objOpps.Win_Lose_Comment__c == null){
                       objOpps.Win_Lose_Comment__c.addError('Please select the value Win/Lose Comment'); 
                    }
                if ( objOpps.Win_Lose_Reason__c == null)
                {
               
                   objOpps.Win_Lose_Reason__c.addError('Please select the value Win/Lose Reason');
                }      
               
            }
        
            else if(objOpps.StageName == 'Closed Lost')
            {
                if ( objOpps.Win_Lose_Comment__c == null){
                        objOpps.Win_Lose_Comment__c.addError('Please fill the  value Win/Lose Comment');
                 } 
                 
                if ( objOpps.Win_Lose_Reason__c == null){
                        objOpps.Win_Lose_Reason__c.addError('Please  fill  the value Win/Lose Reason');
                 }
                 
                  if( objOpps.Type_Lose__c == null)
                {
                  
                  
                      objOpps.Type_Lose__c.addError('Please fill the value Type Lose');
                }
            
            
           // else{}
            
           }     
            
        }
    }  
}
Gokula KrishnanGokula Krishnan
Hi Srinivas,

To write test class for Trigger, need to do Insert, update or delete operation in your test class based on the Trigger code you written.

Try this for your code:
@isTest
Public class OpportunityTriggerHandlerTest{
	@isTest
	Static void TestMeth(){
		Test.StartTest();
		
		Account acc = new account();
		acc.name = 'test'; 
		// similarly bind all the required fields for account here
		insert acc;
		
		Opportunity opp = new Opportunity();
		opp.name = 'Test';
		opp.closeddate = System.today();
		opp.StageName = 'Prospecting';
		opp.accountid = acc.id;
		// similarly bind all the required fields for opportunity here
		insert opp;
		
		opp.StageName = 'Negotiation/Review';
		update opp;
		
		opp.StageName = 'Closed Lost';
		update opp;
		
		Test.StopTest();
	}
}

Thanks,
Gokula Krishnan

If it helps you, please mark is as best answer, so it will be helpful for other developers.