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
Parth SrivastavaParth Srivastava 

Kindly help in test class


public class LightningCustomLookupController{
     @AuraEnabled 
     public static List<sObject> fetchSobject(String searchKeyWord, String sObjectName,String condition) {
         
    
     String searchKey = '%'+searchKeyWord + '%';
     String query = 'SELECT Id, Name FROM '+sObjectName;
     system.debug('condition--------------------'+condition);
     if(sObjectName == 'Order'){
         query = 'SELECT Id, OrderNumber,Account.Name FROM '+sObjectName;
     }
      if(String.isNotBlank(condition)){
        query +=' WHERE '+condition+' LIMIT 100' ; 
      } else {
          query+=' LIMIT 100';
      }
      system.debug('query-----------------------'+query);
      List<sObject> returnList = Database.query(query);
      return returnList;
     }
    @AuraEnabled
    public static sObject getLookUpRecord(Id recordId, String objectName, Boolean isMediaSearch) {
        sObject sObj =  Schema.getGlobalDescribe().get(objectName).newSObject() ;
        if(recordId != null) {
            if(objectName == 'Order'){
                sObj = Database.query('SELECT Id, OrderNumber FROM '+objectName+' WHERE Id =:recordId ');
            } else{
                sObj = Database.query('SELECT Id, Name FROM '+objectName+' WHERE Id =:recordId ');

            }
        }
        return sObj;
    }
    
    @AuraEnabled
    public static List<Account> getRecords(String searchKey) {
        String tempSearchKey = searchKey + '%';
        String ObjectName = 'Account';
        String soql = 'SELECT Name, Type, Phone FROM ' + ObjectName +' WHERE  Name LIKE ' +'\'' + tempSearchKey 
                  +'\'  limit 10';
        system.debug('Query-----------'+soql);
        List<Account> accounts = database.query(soql);
        System.debug( 'accounts->' + accounts);
        return accounts;
    }
}
Raj VakatiRaj Vakati
Try this
 
@isTest
private class CustomLookupControllerTest {

    @isTest static void CustomLookupController_Test() {
       
        Test.startTest();
       
	 Account a = new Account();
    a.Name = 'Test';
	a.NumberOfEmployess =12 ; 
    insert a;

    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    
    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
    
    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    //Test Order Insert
    
    Order o = new Order();
    o.Name = 'Test ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  standardPb.Id ;
    insert o;
    
    OrderItem i = new OrderItem();
    i.OrderId = o.id;
    i.Quantity = 24;
    i.UnitPrice = 240;
    i.Pieces__c = i.Quantity;
    i.Product2id = p.id;
    i.PricebookEntryId=standardPrice.id;
    insert i;


	
	
	   LightningCustomLookupController.fetchSobject('Test','Account','NumberOfEmployess =12');
	   	   LightningCustomLookupController.fetchSobject('Test','Order','NumberOfEmployess =12');

	   LightningCustomLookupController.getLookUpRecord(a.Id,'Account',false);
	   LightningCustomLookupController.getLookUpRecord(o.Id,'Order',true);
	   

	   Test.stopTest();
    }
	
}

 
Parth SrivastavaParth Srivastava
Thanks but it still giving error at line 26 System.QueryException: List has no rows for assignment to SObject.

Please Suggest
 
Raj VakatiRaj Vakati
Use this code and can you check why its failing .. Code is looksgood for me
 
@isTest
private class CustomLookupControllerTest {

    @isTest static void CustomLookupController_Test() {
       
        Test.startTest();
       
	 Account a = new Account();
    a.Name = 'Test';
	a.NumberOfEmployess =12 ; 
    insert a;

    Product2 p = new Product2();
    p.Name = ' Test Product ';
    p.Description='Test Product Entry 1';
    p.productCode = 'ABC';
    p.isActive = true;
    insert p;
    
    Pricebook2  standardPb = [select id, name, isActive from Pricebook2 where IsStandard = true limit 1];
    
    PricebookEntry standardPrice = new PricebookEntry();
    standardPrice.Pricebook2Id = standardPb.Id;
    standardPrice.Product2Id = p.Id;
    standardPrice.UnitPrice = 1;
    standardPrice.IsActive = true;
    standardPrice.UseStandardPrice = false;
    insert standardPrice ;
    
    //Test Order Insert
    
    Order o = new Order();
    o.Name = 'Test ';
    o.Status = 'Draft';
    o.EffectiveDate = system.today();
    o.EndDate = system.today() + 4;
    o.AccountId = a.id;
    o.Pricebook2Id =  standardPb.Id ;
    insert o;
    
    OrderItem i = new OrderItem();
    i.OrderId = o.id;
    i.Quantity = 24;
    i.UnitPrice = 240;
    i.Pieces__c = i.Quantity;
    i.Product2id = p.id;
    i.PricebookEntryId=standardPrice.id;
    insert i;


	
	
	   LightningCustomLookupController.fetchSobject('Test','Account','NumberOfEmployess =12');
	   	   LightningCustomLookupController.fetchSobject('Test','Order','NumberOfEmployess =12');

	   LightningCustomLookupController.getLookUpRecord(a.Id,'Account',false);
	   LightningCustomLookupController.getLookUpRecord(o.Id,'Order',true);
	   LightningCustomLookupController.getRecords('Test');

	   Test.stopTest();
    }
	
}