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
Suraj DemgundeSuraj Demgunde 

i want to write test class for below trigger plz help me

trigger Lead_Update on Lead (before insert,before update) {
    String dam ='';
    String jub='';
    String west1 ='';
    String west2 = '';
    String central = '';
    String Gcc = '';
    
     String uid=UserInfo.getUserId(); 
      User UserDetails =   [select  User.Lead_Region__c, User.Location__c from User Where User.Id =:uid limit 1]; 
       string UserLoc = UserDetails.Location__c;
       string LeadRegion = UserDetails.Lead_Region__c; 
        
    
    for(Lead lt : trigger.new){
        
       lt.Lead_City__c = UserLoc;
       lt.Region__c = LeadRegion;
    
        
    }

}
krisstannum1krisstannum1
here's a rough one
 
@isTest
public class LeadTriggerTest {
	
    @isTest
    static void testInsert(){
        Lead newLead = new Lead();
        newLead.LastName = 'Last Name';
        newLead.Company = 'Company';
        
        test.startTest();
        insert newLead;
        test.stopTest();
        
        System.assertEquals('your expected city for the current logged in user', newLead.Lead_City__c)
    }
    
}

 
Raj VakatiRaj Vakati
Use the below one 
 
@isTest
private class LeadTestClass{
    @isTest
    static void testMethod(){
        
        Profile pf= [Select Id from profile where Name='System Administrator']; 
        
        String orgId=UserInfo.getOrganizationId(); 
        String dateString=String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','') ;
        Integer RandomId=Integer.valueOf(Math.rint(Math.random()*1000000)); 
        String uniqueName=orgId+dateString+RandomId; 
        User uu=new User(firstname = 'ABC', 
                         lastName = 'XYZ', 
                         email = uniqueName + '@test' + orgId + '.org', 
                         Username = uniqueName + '@test' + orgId + '.org', 
                         EmailEncodingKey = 'ISO-8859-1', 
                         Alias = uniqueName.substring(18, 23), 
                         TimeZoneSidKey = 'America/Los_Angeles', 
						 Lead_Region__c='EMAC',
						 Location__c ='USA',
                         LocaleSidKey = 'en_US', 
                         LanguageLocaleKey = 'en_US', 
                         ProfileId = pf.Id
                        ); 
        
        
        insert uu;
        Lead newLead = new Lead();
        newLead.LastName = 'Last Name';
        newLead.Company = 'Company';
		
		insert newLead ;
    }
    
}

 
Amit Chaudhary 8Amit Chaudhary 8
I will recommend you to start using trailhead to learn about test classes
1) https://trailhead.salesforce.com/modules/apex_testing

Pleasse check below post sample test class
1) http://amitsalesforce.blogspot.com/2015/06/best-practice-for-test-classes-sample.html

Also please check below post
1) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
2) https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_example.htm


You write a test class for this the same way that you would any other:
- Set up some data for the Trigger to access
- Verify the behaviour with asserts.
Try below code
@isTest
private class TestRunAs {
   public static testMethod void testRunAs() 
   {
        String uniqueUserName = 'standarduser' + DateTime.now().getTime() + '@testorg.com';
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];

        User u = new User(Alias = 'standt', Email='standarduser@testorg.com',
        EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
        LocaleSidKey='en_US', ProfileId = p.Id,
        TimeZoneSidKey='America/Los_Angeles',
         UserName=uniqueUserName , Lead_Region__c ='Test' , Location__c ='Test');

        System.runAs(u) {
			
			Lead newLead = new Lead();
				newLead.LastName = 'Last Name';
				newLead.Company = 'Company';
			insert newLead ;
			
			List<Lead> lstLead = [Select id ,Lead_City__c ,Region__c from LEAD where id=:newLead.id];
			System.assert(lstLead.size() > 0 );
			System.assert(lstLead[0].Lead_City__c =='Test' );
			
        }
    }
}

Let us know if this will help you