+ Start a Discussion
Ummang JainUmmang Jain 

controller testclass needed

hey how should i write test class for this controller

global class AutoCompleteLookupController {
    
    @RemoteAction
    global static SObject[] findSObjects(string obj, string qry,Integer recordCount,String accountId, string addFields) {
        // more than one field can be passed in the addFields parameter
        // split it into an array for later use
        List<String> fieldList;
        if (String.isNotBlank(addFields)) fieldList = addFields.split(',');
       // check to see if the object passed is valid
        Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
        Schema.SObjectType sot = gd.get(obj);
        if (sot == null) {
            // Object name not valid
            return null;
        }
        // create the filter text
        String filter = ' like \'%' + String.escapeSingleQuotes(qry) + '%\'';
        //begin building the dynamic soql query
        String soql = 'select id, Name';
        // if an additional field was passed in add it to the soql
        if (fieldList != null) {
            for (String s : fieldList) {
                soql += ', ' + s;
            }
        }
        // add the object and filter by name to the soql
       if(recordCount > 15){
          soql += ' from ' + obj + ' where Company__c =:accountId AND name' + filter;
       }else{
           soql += ' from ' + obj + ' where Company__c =:accountId';
       }
        // add the filter by additional fields to the soql
        if (fieldList != null && recordCount > 15) {
            for (String s : fieldList) {
                soql += ' or ' + s + filter;
            }
        }
        soql += ' order by Name limit 20';
        List<sObject> L = new List<sObject>();
        try {
            L = Database.query(soql);
        }
        catch (QueryException e) {
            return null;
        }
        return L;
   }
}



 
BALAJI CHBALAJI CH
Hi Ummang Jain,
Please find below Test Class for the controller.
 
@isTest
public class Test_AutoCompleteLookupController {
    
    //Test Method to cover if recordcount < 15
    public static testmethod void test1()
    {
        Account ac = new Account (name='TestAccount');
        Insert ac;
        
        AutoCompleteLookupController.findSObjects('Account', 'Hello', 20, ac.id, 'Id, Name');
    }
    
    //Test Method to cover if recordcount > 15
    public static testmethod void test2()
    {
        Account ac = new Account (name='TestAccount');
        Insert ac;
        
        AutoCompleteLookupController.findSObjects('Account', 'Hello', 10, ac.id, 'Id, Name');
    }
    
    //Test Method to cover if sot == null
    public static testmethod void test3()
    {
        Account ac = new Account (name='TestAccount');
        Insert ac;
        
        AutoCompleteLookupController.findSObjects('', 'Hello', 20, ac.id, 'Id, Name');
    }
}

Let us know if that helps you.

Best Regards,
BALAJI
 
Tarun SuriTarun Suri
Hi Ummang
Please find below the test class of Your class
@isTest
public class AutoCompleteLookupController_Test {
	public static testMethod void methodname1()
        {
            account acc= new account();
            acc.Name = 'Tarun';
            insert acc;
            
            string obj = 'Account';
            string qry= 'Alpha';
            Integer recordCount=16;
            String accountId=acc.id; 
            string addFields = 'Name';
            
            AutoCompleteLookupController acl = new AutoCompleteLookupController();
            SObject[] s = AutoCompleteLookupController.findSObjects(obj,qry,recordCount,accountId,addFields);
        }
}

Let me Know If that helps you

Thanks
Tarun Suri