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
Miguel RoaMiguel Roa 

Writing my first test class

Hello, I'm new to Salesforce and I've been told to write a test class for this class and frankly I don't know where to start.

The thing is that the examples I've found on trailblaze only cover really simple classes with numeric returns.

Could you please provide some guidance? Thanks in advance.

This is the aforementioned class:
public class BlacklistLeadValidation {
    
    public static string errorMsg { 
        get { 
            return 'Your attempt to change the [FieldName] status was not successful due to insufficient privileges. Please send a chatter post to #help with supporting documentation for the change request.';
        }
    }
    
    public static void OptInWireless_Wireline(Map<Id, Lead> leadRecord, Map<Id, Lead> leadRecordOld)
    {
        if (leadRecord != null && leadRecordOld != null)
        {
            List<Profile> currentUserProfile  = [Select Id, Name From Profile Where Id = :UserInfo.getProfileId()];
            if (currentUserProfile.size() > 0)
            {
                if (!Label.AP_VBM_ApprovalAllowedProfiles.containsIgnoreCase(currentUserProfile[0].Name)) {
                    for(id recId : leadRecord.keySet())
                    {
                        Lead leadOld = leadRecordOld.get(recId);
                        Lead leadNew = leadRecord.get(recId);
                        
                        // Wireline consent validation (booleans cannot moved from true to false)
                        if (leadOld.Do_Not_Call__c == true && leadNew.Do_Not_Call__c == false)
                        {
                            leadNew.Do_Not_Call__c.addError(errorMsg.replace('[FieldName]', 'Do Not Call'));
                        }
                        
                        if (leadOld.Do_Not_Mail__c == true && leadNew.Do_Not_Mail__c == false)
                        {
                            leadNew.Do_Not_Mail__c.addError(errorMsg.replace('[FieldName]', 'Do Not Mail'));
                        }
                        
                        
                        if (leadOld.HasOptedOutOfEmail == true && leadNew.HasOptedOutOfEmail == false)
                        {
                            leadNew.HasOptedOutOfEmail.addError(errorMsg.replace('[FieldName]', 'Do Not Email'));
                        }
                        
                        
                        
                        // Wireless consent validation (booleans cannot moved from true to false)
                        if (leadOld.Do_Not_Email_VZW__c == true && leadNew.Do_Not_Email_VZW__c == false)
                        {
                            leadNew.Do_Not_Email_VZW__c.addError(errorMsg.replace('[FieldName]', 'Do Not Email Wireless'));
                        }
                        
                        if (leadOld.Do_Not_Call_VZW__c == true && leadNew.Do_Not_Call_VZW__c == false)
                        {
                            leadNew.Do_Not_Call_VZW__c.addError(errorMsg.replace('[FieldName]', 'Do Not Call Wireless'));
                        }
                        
                        if (leadOld.Do_Not_Mail_VZW__c == true && leadNew.Do_Not_Mail_VZW__c == false)
                        {
                            leadNew.Do_Not_Mail_VZW__c.addError(errorMsg.replace('[FieldName]', 'Do Not Mail Wireless'));
                        }
                        
                        // Not Opt In cannot be change either for wireline or wireless
                        if (leadOld.Email_Opt_In__c == 'No Opt In' && leadNew.Email_Opt_In__c != 'No Opt In')
                        {
                            leadNew.Email_Opt_In__c.addError(errorMsg.replace('[FieldName]', 'Email Opt In'));
                        }
                        
                        if (leadOld.Email_Opt_In_VZW__c == 'No Opt In' && leadNew.Email_Opt_In_VZW__c != 'No Opt In')
                        {
                            leadNew.Email_Opt_In_VZW__c.addError(errorMsg.replace('[FieldName]', 'Email Opt In Wireless'));
                        }
                        
                    }
                }
            }  
        }
        
    }
    
}

 
Best Answer chosen by Miguel Roa
Suraj Tripathi 47Suraj Tripathi 47

Hi Please find the test class

@isTest
public class BlacklistLeadValidation {

public static testMethod void OptInWireless_WirelineTest(){
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='standarduser@testorg.com');
			
        Map<id,lead> newmap = new Map<id,lead>();
        Map<id,lead> oldmap = new Map<id,lead>();
		List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open',
						  Do_Not_Call__c =false,Do_Not_Mail__c =false,HasOptedOutOfEmail =false,
						  Do_Not_Email_VZW__c =false,Do_Not_Call_VZW__c =false,Do_Not_Mail_VZW__c =false,Email_Opt_In__c ='false',Email_Opt_In_VZW__c=false ),
                          new Lead(Company = 'Nike', LastName = 'John', Status = 'Open'),
                          new Lead(Company = 'Miles', LastName = 'Davis', Status = 'Open'),
                          new Lead(Company = 'Reebok', LastName = 'Hillen', Status = 'Open'),
                          new Lead(Company = 'Addidas', LastName = 'Shrin', Status = 'Open')
                         };  
        insert lstLead;
		
		for(Lead ld:lstLead){
		newmap.put(ld.id,ld);
		}
		
         lstLead[0].LastName='Update Data';
		 lstLead[0].Do_Not_Call__c=true;
		 lstLead[0].Do_Not_Mail__c=true;
		  lstLead[0].HasOptedOutOfEmail=true;
		  lstLead[0].Do_Not_Email_VZW__c=true;
		 		  lstLead[0].Do_Not_Mail_VZW__c =true;
		  lstLead[0].Email_Opt_In__c =true;
		  
		  lstLead[0].Email_Opt_In_VZW__c =true;
		   
		 update lstLead[0];
		 
		 oldmap.put(lstLead[0].id,lstLead[0]);
 
        Test.startTest();
        BlacklistLeadValidation.OptInWireless_Wireline(newmap,oldmap);
        Test.stopTest();
        
   		
		 
}

}

do some need full changes according to your code.
Please mark it as the Best Answer if it helps you.

Thank You

All Answers

Suraj Tripathi 47Suraj Tripathi 47

Hi Please find the test class

@isTest
public class BlacklistLeadValidation {

public static testMethod void OptInWireless_WirelineTest(){
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='standarduser@testorg.com');
			
        Map<id,lead> newmap = new Map<id,lead>();
        Map<id,lead> oldmap = new Map<id,lead>();
		List<Lead> lstLead =   new List<Lead>{
                          new Lead(Company = 'JohnMiller', LastName = 'Mike', Status = 'Open',
						  Do_Not_Call__c =false,Do_Not_Mail__c =false,HasOptedOutOfEmail =false,
						  Do_Not_Email_VZW__c =false,Do_Not_Call_VZW__c =false,Do_Not_Mail_VZW__c =false,Email_Opt_In__c ='false',Email_Opt_In_VZW__c=false ),
                          new Lead(Company = 'Nike', LastName = 'John', Status = 'Open'),
                          new Lead(Company = 'Miles', LastName = 'Davis', Status = 'Open'),
                          new Lead(Company = 'Reebok', LastName = 'Hillen', Status = 'Open'),
                          new Lead(Company = 'Addidas', LastName = 'Shrin', Status = 'Open')
                         };  
        insert lstLead;
		
		for(Lead ld:lstLead){
		newmap.put(ld.id,ld);
		}
		
         lstLead[0].LastName='Update Data';
		 lstLead[0].Do_Not_Call__c=true;
		 lstLead[0].Do_Not_Mail__c=true;
		  lstLead[0].HasOptedOutOfEmail=true;
		  lstLead[0].Do_Not_Email_VZW__c=true;
		 		  lstLead[0].Do_Not_Mail_VZW__c =true;
		  lstLead[0].Email_Opt_In__c =true;
		  
		  lstLead[0].Email_Opt_In_VZW__c =true;
		   
		 update lstLead[0];
		 
		 oldmap.put(lstLead[0].id,lstLead[0]);
 
        Test.startTest();
        BlacklistLeadValidation.OptInWireless_Wireline(newmap,oldmap);
        Test.stopTest();
        
   		
		 
}

}

do some need full changes according to your code.
Please mark it as the Best Answer if it helps you.

Thank You

This was selected as the best answer
Suraj Tripathi 47Suraj Tripathi 47

and for this 

public static string errorMsg { 
        get { 
            return 'Your attempt to change the [FieldName] status was not successful due to insufficient privileges. Please send a chatter post to #help with supporting documentation for the change request.';
        }
    }


test class:

Test.startTest();
String data=BlacklistLeadValidation.errorMsg;
 BlacklistLeadValidation.OptInWireless_Wireline(newmap,oldmap);
Test.stopTest();