+ Start a Discussion
ChiyanChiyan 

Help me to write a test class below code

public class Lookup {

    
    @AuraEnabled 
    public static String searchDB(String objectName, String fld_API_Text, String fld_API_Val, 
                                  Integer lim,String fld_API_Search,String searchText ){
        
        searchText='\'%' + String.escapeSingleQuotes(searchText.trim()) + '%\'';

        
        String query = 'SELECT '+fld_API_Text+' ,'+fld_API_Val+
                        ' FROM '+objectName+
                            ' WHERE '+fld_API_Search+' LIKE '+searchText+ 
                        ' LIMIT '+lim;
        
        List<sObject> sobjList = Database.query(query);
        List<ResultWrapper> lstRet = new List<ResultWrapper>();
        
        for(SObject s : sobjList){
            ResultWrapper obj = new ResultWrapper();
            obj.objName = objectName;
            obj.text = String.valueOf(s.get(fld_API_Text)) ;
            obj.val = String.valueOf(s.get(fld_API_Val))  ;
            lstRet.add(obj);
        } 
         return JSON.serialize(lstRet) ;
    }
    
    public class ResultWrapper{
        public String objName {get;set;}
        public String text{get;set;}
        public String val{get;set;}
    }
}
Raj VakatiRaj Vakati
try this 
 
@isTest
private class myClass {
    static testMethod void myTest() {
       
          Account acc = new Account(Name='Test Account',Phone='123434343',Rating='Cold');
		  
          insert acc;
     
	 Lookup.searchDB('Account','Id','Name'12,'Name','Test');
	 
	 
	 }
}

 
Raj VakatiRaj Vakati
Use this pls
 
@isTest
private class LookupTest {
    static testMethod void myTest() {
        
        Account acc = new Account(Name='Test Account',Phone='123434343',Rating='Cold');
        
        insert acc;
        
        Lookup.searchDB('Account','Id','Name',12,'Name','Test');
        
        
    }
}

 
Deepali KulshresthaDeepali Kulshrestha
Hi Chiyan,

@isTest public class Lookup_Test {
@isTest static void lookupTest()
{
    Test.startTest();
    Contact con = new Contact(FirstName='Test',LastName='Contact');
          
          insert con;
     
     Lookup.searchDB('Contact','Id','Name',12,'Name','Test');
     
    
    Test.stopTest();
}
}


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha

 
Ajay K DubediAjay K Dubedi
Hi Chiyan,
@isTest
public class testLookup  {
 @isTest
    private static void testSearchDB() {
          Account acc = new Account(Name='Test_Account',Phone='12345');
   insert acc;
   Test.startTest();
   Lookup.searchDB('Account','Id','Name'12,'Name','Test');
   Test.stopTest();
     }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
 
Tad Aalgaard 3Tad Aalgaard 3
He is asking you to to do his work.  If you read the other posts this person has started you will see that he has asked many times for others to provide him with a test class.  Please do not do his work for him.  He needs to do Trailheads and read how to create Test classes and learn how to create these himself.