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 91Guru 91 

Apex controller test class

--------I am not getting how to write a test calss for this apex controller?
public class OV_ObjectUtility {
    public static void getData(){
        List<String> ids = new List<String>();
        ids.add('001U0000004ZjhgIAC');
        ids.add('0010B00001lL1VEQA0');
        ids.add('001e0000018SOK5AAO');
        ids.add('001e0000018SOKAAA4');
        lightningTableWrapper ffff= getRecordsForSearch('Account','Name, OriginalAccountName__c, TradestyleName__c, Continent__c, BillingCity, BillingCountry, website, BillingPostalCode, BillingState, BillingStreet, Id',ids);
        
        system.debug('----Final Query Datas ---'+ffff); 
    }
    public static lightningTableWrapper getRecordsForSearch(String ObjectName,String fieldstoget,List<String> ids){     
        lightningTableWrapper ltw = new lightningTableWrapper();
        String wherecon='';
        try{            
            for(String str : ids){
                if(str!=null){
                    wherecon = wherecon+ ' \''+str + '\',' ;
                }
            }
            wherecon = wherecon.subString(0,wherecon.length()-1);
            String queryString = 'Select '+ String.escapeSingleQuotes(fieldstoget)+
                ' from '+ String.escapeSingleQuotes(ObjectName)+
                ' where id in ('+wherecon+')';
            system.debug('----Final Query---'+queryString);
       
            List<sobject> sObjectsList = new List<sobject>();
           
            sObjectsList.addAll(database.query(queryString));
            system.debug('----Final Query Datas ---'+sObjectsList);
            ltw.sObjectrecords = sObjectsList;
        }catch(Exception e){
        }
        return ltw;
    }
}
Best Answer chosen by Guru 91
Akshay_DhimanAkshay_Dhiman
Hi Guru,
Do not use Hard code is in your apex class.
After removing hard code ids try this way out for reaching your code coverage.

Try this way out for your Test class code -- 
 
@isTest
public class OV_ObjectUtility_Test
{
@isTest static void getData_Test()
{
List<Account> accList = new List<Account>();

for(Integer i=0 ; i<=10 ; i++)
{
Account acc = new Account();
acc.Name = 'Test Acc' + i;
accList.add(acc);
}
if(accList.size()>0)
{
insert accList;
}
Test.startTest();
OV_ObjectUtility.getData();
Test.stopTest();
}
}


Thanks 
Akshay

All Answers

Pritesh MaturkarPritesh Maturkar
Hello Guru,

There are many way to create test class for apex.

Please refer  follow link 
https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro

If any other question then you can post it,

Regards,
Pritesh 
Guru 91Guru 91
Hi Pritesh , I acheived the code coverage. Thank you
Akshay_DhimanAkshay_Dhiman
Hi Guru,
Do not use Hard code is in your apex class.
After removing hard code ids try this way out for reaching your code coverage.

Try this way out for your Test class code -- 
 
@isTest
public class OV_ObjectUtility_Test
{
@isTest static void getData_Test()
{
List<Account> accList = new List<Account>();

for(Integer i=0 ; i<=10 ; i++)
{
Account acc = new Account();
acc.Name = 'Test Acc' + i;
accList.add(acc);
}
if(accList.size()>0)
{
insert accList;
}
Test.startTest();
OV_ObjectUtility.getData();
Test.stopTest();
}
}


Thanks 
Akshay
This was selected as the best answer