Skip to main content
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;

    }

}
3 answers
  1. Mar 30, 2018, 4:00 AM
    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
0/9000