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
Chanagan SakulteeraChanagan Sakulteera 

How to write test class trigger and apex class?

Hi all, I have some experience with apex class but I don't understand how to coding with test class.
If you have any assisting or link that I would better, I really appreciate it.

Last of all, I put some of my trigger in the below to be example to get clearer picture about test class coding.
trigger trgLead on Lead (before insert,before update) {

    Map<String, Lead> leadMap = new Map<String, Lead>();
    for(Lead lead : System.Trigger.new){
    
        // Make sure we don't treat an email address that
        // isn't changing during an update as a duplicate. 
        if((lead.Email != null)&&(System.Trigger.isInsert || (lead.Email != System.Trigger.oldMap.get(lead.Id).Email))){
            
            //Make sure another new lead isn't also a duplicate
            
            if(leadMap.containsKey(lead.Email)){
                lead.Email.addError('Another new lead has the ' + ' same email address.');
            }else{
                leadMap.put(lead.Email, lead);
            }
        }
    }
    
    // Using a single database query, find all the leads in 
    // the database that have the same email address as any 
    // of the leads being inserted or updated.         
    
    for(Lead lead : [Select Email from Lead where Email in : leadMap.KeySet()]){
        Lead newLead = leadMap.get(lead.Email);
        newLead.Email.addError('A lead with this email ' + ' address already exist.');
    }
}
This trigger have check dupdicate in lead.
Sudeep DubeSudeep Dube
As you require the Link where u can study and that will Guide u to write test Class

http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/

my point of view u have to create a data factory based on  all test scenario then apply insert and update that will help to full covered as well as well tested code ...