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
Abhilash DaslalAbhilash Daslal 

test method for the following method

Hi Guys,
Please give the test class for the following class

​Public class getAccounts{

private String getAccountRecords(String AccuntId, String accountFor){
String recordId;
List<Account> accList =[Select id,Name,phone,Email from Account LIMIT 1];

if(!accList.isEmpty()){
recordId = accList[0].id;
    }

    return recordId;
    }
}
Best Answer chosen by Abhilash Daslal
Sagar PatilSagar Patil
Hi Abhilash

You will have to add @testVisible annotation in your controller as the method is private. PFB the test class which will give you 100% code coverage.

Apex class:
 
Public class getAccounts{

@testVisible private String getAccountRecords(String AccuntId, String accountFor){
String recordId;
List<Account> accList =[Select id,Name,phone from Account LIMIT 1];

if(!accList.isEmpty()){
recordId = accList[0].id;
    }

    return recordId;
    }
}

Test class:
 
@isTest
Public class getAccounts_Test{

   public static testMethod void myTest(){
   
      Account acc1 = new Account();
      acc1.Name='acc1';
      insert acc1;
      
      getAccounts obj = new getAccounts();
        
      Test.startTest();
      String id = obj.getAccountRecords(acc1.id, 'XYZ');
      System.assertEquals(id, acc1.id);
      Test.stopTest();
   
   }

}

Note: I have took value of parameter accountFor as XYZ for test purpose. You can modify as per your requirement. Also there is no standard field - Email present on Account object, if it is custom field then please append __c and in test class set some value i.e.acct.email__c='abc@ss.com'

Kindly mark this as a best answer if it helps you.

Regards,
Sagar

All Answers

Sagar PatilSagar Patil
Hi Abhilash

You will have to add @testVisible annotation in your controller as the method is private. PFB the test class which will give you 100% code coverage.

Apex class:
 
Public class getAccounts{

@testVisible private String getAccountRecords(String AccuntId, String accountFor){
String recordId;
List<Account> accList =[Select id,Name,phone from Account LIMIT 1];

if(!accList.isEmpty()){
recordId = accList[0].id;
    }

    return recordId;
    }
}

Test class:
 
@isTest
Public class getAccounts_Test{

   public static testMethod void myTest(){
   
      Account acc1 = new Account();
      acc1.Name='acc1';
      insert acc1;
      
      getAccounts obj = new getAccounts();
        
      Test.startTest();
      String id = obj.getAccountRecords(acc1.id, 'XYZ');
      System.assertEquals(id, acc1.id);
      Test.stopTest();
   
   }

}

Note: I have took value of parameter accountFor as XYZ for test purpose. You can modify as per your requirement. Also there is no standard field - Email present on Account object, if it is custom field then please append __c and in test class set some value i.e.acct.email__c='abc@ss.com'

Kindly mark this as a best answer if it helps you.

Regards,
Sagar
This was selected as the best answer
Nayana KNayana K
​Public class getAccounts{
@TestVisible
private String getAccountRecords(String AccuntId, String accountFor){
String recordId;
List<Account> accList =[Select id,Name,phone,Email from Account LIMIT 1];

if(!accList.isEmpty()){
recordId = accList[0].id;
    }

    return recordId;
    }
}
Change your class like above (Added @TestVisible because the method is private)
String AccuntId, String accountFor parameters you are not using inside the method! so, no use of them.
 
@isTest
public class AccountTest
{
	@isTest
    public static void test()
	{
		// insert sample data
		Account objA = new Account(Name ='TestAcc');
		insert objA;
		
		// call the method
		getAccounts inst = new getAccounts();
		String recordId = inst.getAccountRecords('','');
		
		// verify the output
		system.assertEquals(recordId, objA.Id);
		
	}
}


 
Abhilash DaslalAbhilash Daslal
Thank you @Sagar Patil and @Nayana K for your quick replies. Helped a lot

Thanks,
Abhilash