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
Madan RamachandranMadan Ramachandran 

i have 1 doubt in SFDC testing

how to write the test class for trigger
ASIF ALIASIF ALI
Hiii Madan,
For writing test class we have to perform the actions that fire the trigger and verify expected results.
check out this Example
 
trigger DmlTriggerNotBulk on Account(after update) {   
    // Get the related opportunities for the accounts in this trigger.        
    List<Opportunity> relatedOpps = [SELECT Id,Name,Probability FROM Opportunity
        WHERE AccountId IN :Trigger.New];          
    // Iterate over the related opportunities
    for(Opportunity opp : relatedOpps) {      
        // Update the description when probability is greater 
        // than 50% but less than 100% 
        if ((opp.Probability >= 50) && (opp.Probability < 100)) {
            opp.Description = 'New description for opportunity.';
            // Update once for each opportunity -- not efficient!
            update opp;
        }
    }    
}

Test class for this trigger
trigger DmlTriggerBulk on Account(after update) {   
    // Get the related opportunities for the accounts in this trigger.        
    List<Opportunity> relatedOpps = [SELECT Id,Name,Probability FROM Opportunity
        WHERE AccountId IN :Trigger.New];
          
    List<Opportunity> oppsToUpdate = new List<Opportunity>();
    // Iterate over the related opportunities
    for(Opportunity opp : relatedOpps) {      
        // Update the description when probability is greater 
        // than 50% but less than 100% 
        if ((opp.Probability >= 50) && (opp.Probability < 100)) {
            opp.Description = 'New description for opportunity.';
            oppsToUpdate.add(opp);
        }
    }
    
    // Perform DML on a collection
    update oppsToUpdate;
}

​​​​​​​
Khan AnasKhan Anas (Salesforce Developers) 
Hi Madan,

Greetings to you!

Please refer to the below links which might help you further with the above requirement.

https://trailhead.salesforce.com/en/content/learn/modules/apex_testing/apex_testing_triggers

https://mindmajix.com/test-class-with-example-in-salesforce

https://salesforce.stackexchange.com/questions/10988/how-to-write-a-unit-test-test-class-for-trigger

http://www.sfdcpoint.com/salesforce/test-class-with-example-salesforce/

https://www.salesforcetutorial.com/write-test-class-for-trigger-simpleexample/

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas