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
Indhurani KanagarajIndhurani Kanagaraj 

How to write Apex test class for this? Please help me

public class Handeller
{
    List<ID> tempAccList = new List<ID>();
     List<Account> aList = new List<Account>();
      List<Customer__c> cList = new List<Customer__c>(); 
      List<Account> aList1 = new List<Account>();
      
      public void getAccountId (List<Account> accList)
      {
          for (Account acc: accList)
          {
              tempAccList.add(acc.Id);
          }    
          if(!tempAccList.isEmpty())
          {
              createCustomer();
          }
       }
       
        public void createCustomer()
        {
          aList =[SELECT Id,Name,Phone,Role_of_the_main_contact__c FROM account WHERE Id IN:tempAcclist];
          for (Account acc1: aList)
          {
              Customer__c cust = new Customer__c();
              cust.Name = acc1.Name;
              cust.Phone__c= acc1.Phone;
              cust.Role_of_the_contact__c=acc1.Role_of_the_main_contact__c;
              cust.Related_Account__c=acc1.ID;
              cList.add(cust);
              
              acc1.Phone=null;
              acc1.Role_of_the_main_contact__c=null;
              aList1.add(acc1);
          }
          
          insert cList;
          update aList1;
          
        } 
}
Best Answer chosen by Indhurani Kanagaraj
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 Apex to access
- Verify the behaviour with asserts.
@isTest 
public class HandellerTest 
{
    static testMethod void testMethod1() 
	{
		List<Account> lstAcc = new List<Account>();

		Account acc = new Account();
		acc.Name='Test';
		
		Account acc1 = new Account();
		acc1.Name='Test';
		lstAcc.add(acc1);
		
		insert lstAcc;
		
		Handeller obj = new Handeller();
		obj.getAccountId(lstAcc);
		
		
    }
}


Let us know if this will help you