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
Guru Vemuru 1Guru Vemuru 1 

Can any one tell me how to write test class?

Hi everyone,
I want write test class how to write test class
JohnHernandez.ax1746JohnHernandez.ax1746
The Get Started with Apex Unit Tests (https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro) trailhead explains the process.
Sachin P Sam 1Sachin P Sam 1
for writing the test class ,start your class with @isTest annoatation,followed by the name of the class.(1)
Inside the class ,call the method preceded by the @isTest annoatation.When calling, pass the values needed to test the method.

Eg:
@isTest 
private class TemperatureConverterTest { 
@isTest static void testWarmTemp() {
Decimal celsius = TemperatureConverter.FahrenheitToCelsius(70);   System.assertEquals(21.11,celsius); }

comment if having doubt regarding this.
select this as best,if helped you.

Thanks!.
Ajay K DubediAjay K Dubedi
Hi Guru,

The Apex testing framework enables you to write and execute tests for your Apex classes and triggers on the Force.com platform. Apex unit tests ensure high quality for your Apex code and let you meet requirements for deploying Apex.
I hope this code will help you lot to understand and write test class :

Suppose I have to write Test class this Apex code:

Apex Class :
public with sharing class ContactLookUp1 {
  public static void createLookUp1(){
  	list<account> aclist= new list<account>();
  	Integer n=0;
  	aclist=[select Id, Name, NumberOfEmployees from account limit 10]; // Query to get 10 Records of Account from Org
  	list<contact> conlist= new list<contact>();
  	for(account ac:aclist){
  		contact c=new contact();
  		c.LastName='Ajay '+n;
  		n++;
  		c.AccountId=ac.Id; // Creating LookUp of Account in Contact 
  		conlist.add(c); 
  	}
  	if(conlist.size()>0)
  	insert conlist;
  	System.debug(conlist); // Test class does not Execute the 'debug' line
  }   
}

Test Class Code for Above Apex Code : 
@isTest 
public with sharing class ContactLookUp1ForTestClass{
    @isTest static void testMethodCaseRecord(){
    	Account ac = new Account(); // Creating a  New Account for Test
    	ac.Name = 'Guru Vemuru';
    	ac.NumberOfEmployees = 5;
     	insert ac;
    	system.assert(true,'Account is Inserted');
       	Integer n=0;
   		test.startTest(); // Predefined Method for Test Class to Start the Test Execution of Apex Code
    	ContactLookUp1.createLookUp1(); // Calling the Class and their Method for Test
    	test.stopTest(); // Predifined Method to Stop Test Execution 
    }
}
User-added image

Points to remember for Test Classes:
1. Test class always Start with @isTest
2. Test class do not Execute System.debug()
3. test.startTest() and test.stopTest() must be used in Test Class.

For More Information go through these links
https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_qs_test.htm
http://www.sfdc99.com/2013/05/14/how-to-write-a-test-class/

Let me know if you have any Question.
If you understand, then mark it as best answer.
Thank You

With Regards,
Ajay