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
salesforcemicky125salesforcemicky125 

Unable to cover the code coverage for the inner class using Test class in Apex

Hi Jeff,

Need your help .

I have written a  test class where i was unable to cover the code coverage for the Inner class.

I have a class which have 2 class in one class . when i have  written a test class i was unablk to cover the code coverage 1 class.

This is my controller :

global class JobSearch
{
    public String searchemail {get;set;}
    public string persearch{get;set;}
    public string jobDesc{get;set;}
    public List<Job__c> objJob{get;set;}
    public Job__c newjob{get;set;} 
    public List<List<Sobject>> objJobList{get;set;} 
    public JobSearch(ApexPages.StandardController controller)
    {
    List<Job__c> ObjJob= new List<Job__c>();
   
    List<List<Sobject>> objJobList = new List<List<Job__c>>(); 
     newjob = new Job__c();
    objJob=[Select Job_ID__c,Id,Name,Account__r.Name,CCL_Job_Type__c,Post_Date__c,Industry__c,Job_Category__c,Career_Level__c,AR_Salary__c,AR_Job_Details__c,AR_Job_Application_Link__c from Job__c where Name like :searchemail limit 1];

    //objJobList = [FIND 'Name' IN ALL FIELDS RETURNING Job__c(Id,Name,Account__c,Industry__c,Job_Category__c,Career_Level__c)];
    }
  public PageReference search()
    {
     try
       {
   
    persearch = '%'+searchemail+'%';
   // objJob=[Select Id,Name,Account__c,CCL_Job_Type__c,Post_Date__c,Industry__c,Job_Category__c,Career_Level__c,AR_Salary__c,AR_Job_Details__c from Job__c where Name like :persearch or Industry__c like :persearch or Account__r.Name like :persearch or CCL_Job_Type__c like :persearch or Job_Category__c like :persearch limit 10000];
    objJob=[Select Description__c,Job_ID__c,Id,Name,Account__r.Name,CCL_Job_Type__c,Post_Date__c,Industry__c,Job_Category__c,Career_Level__c,AR_Salary__c,AR_Job_Details__c,AR_Job_Application_Link__c from Job__c where Name like :persearch or Industry__c like :persearch or Account__r.Name like :persearch or FSS_CCL__c like :persearch or Location__c like :persearch limit 10000];

     objJobList = [FIND 'Name'  IN ALL FIELDS RETURNING Job__c(Id,Name,Account__c,Industry__c,Job_Category__c,Career_Level__c)];   
   
   
    }
    catch(Exception ex){
          System.debug('\n\nException ='+ex.getMessage()+'\n\n');
          searchemail ='';

    

     }
    return null;
    }
   
   public String searchedRecordId { get; set; }
    public static list<ResultSet> searchedRecord {get;set;}   
    @RemoteAction
    global static ResultSet[] getRecords(String searchText) {
        //sObject List
        searchedRecord = new list<ResultSet>();
        //SOSL Text should be more then one charecter
        if(searchText.length() >0){
            //SOSL opretion to retrive records of the Account, Lead, Contact, Opportunity, Lead Objects you can add more.
            List<List<SObject>> searchList = [FIND :searchText IN ALL FIELDS RETURNING Job__c(Id,Name,Industry__c)];
            //Adding diffrent object's records in sobject list
            for(List<SObject> o:searchList){
                for(SObject s:o){
                    searchedRecord.add(new JobSearch.ResultSet(s));
                }
            }
        }

        return searchedRecord;
    }

      global class ResultSet{
        public String Id {get;set;}
        public String Name{get;set;}
        //public String Level__c{get;set;}
      // public String Description {get;set;}
       // public String sObjectName {get;set;}
        public ResultSet(sObject s){
            this.Id = s.Id;
            this.Name = s.get('Name')+'';
          //  this.Level__c=s.get('City__c')+'';
          //  this.Description = s.get('Description')+'';
            //this.sObjectName = getsObjectNameById(Id);           
        }      
        global ResultSet(String Id,String Name){
            this.Id = Id;
            this.Name = Name;
           //this.Level__c=City__c;
            //this.Description = Description;
            //this.Description__c= Description__c;
            //this.sObjectName = getsObjectNameById(Id);           
        }
 
    }   
   
           }


Test class

@isTest(SeeAllData=true)
private class TestJobSearch
{        
private static testMethod void TestJobSearch()
      {
         contact con=new contact(Lastname='A',Email='ryeturi@deloitte.com',FirstName='Srikanth',Phone='9620566619');
         insert con;
         List<Job__c> objJob = new List<Job__c>();
        objJob=[Select Job_ID__c,Id,Name,Account__r.Name,CCL_Job_Type__c,Post_Date__c,Industry__c,Job_Category__c,Career_Level__c,AR_Salary__c,AR_Job_Details__c,AR_Job_Application_Link__c from Job__c limit 1];
   
      Asset assetObj = new Asset(Name = 'Test Asset');
      insert assetObj;
   
       ApexPages.StandardController sc = new ApexPages.StandardController(con);   
       JobSearch createCon = new JobSearch(sc);
             
       PageReference pageRef = Page.jobsearchrepeat;
       Test.setCurrentPage(pageRef);
                 
                   createCon.search();
    }
       }

The Class is fine worked for the Upper class ,but i cannot able to call the 2nd class which is in Bold letters.........
My code coverage is 47% only , can anyone help me out from this so that i can cover min 75% code coverage to move it to production
Ashish_SFDCAshish_SFDC
Hi , 


That generally means that the dummy records used for test coverage is calling the inner class. 


See below for more information, 


Wherever you have call that inner class, its code covereage gonna be covered same like Wrapper classes codes get covered,

But if not you can call them like

MainClass.InnerClass innerController = new MainClass.InnerClass();

You can check this blog for your help
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html


https://developer.salesforce.com/forums?id=906F000000094R1IAI

Also See the thread, 

http://salesforce.stackexchange.com/questions/7696/weird-issue-with-inner-class-not-being-covered-via-test-class


Regards,
Ashish
khillan bhardwajkhillan bhardwaj
Hi Jeff,

To cover code of inner class you need to cover apex code where we instantiate inner class .if this is not posible then you need to create object of inner class explicitly like below : 

JobSearch.ResultSet rs = new JobSearch.ResultSet (param1 ,param2);//this is for 2nd constructor
JobSearch.ResultSet rs = new JobSearch.ResultSet (param1of SObjecType);

Thanks
salesforcemicky125salesforcemicky125
Hi Jeff,

 Thank you so much for replying me ............

I have implemented the code you have sent me..........

It's working fine now i can call the Second class(i.e )  inner class Now i have my code coverage uptp 70 % need another 10 % of code covergae to move it to the production.

That would be very helpful .


This the code where i was unable to cover the Code covergae. Could you please help me out from this . This would be very helpful.

public String searchedRecordId { get; set; }
    public static list<ResultSet> searchedRecord {get;set;}   
    @RemoteAction
    global static ResultSet[] getRecords(String searchText) {
        //sObject List
        searchedRecord = new list<ResultSet>();
        //SOSL Text should be more then one charecter
        if(searchText.length() >0){
            //SOSL opretion to retrive records of the Account, Lead, Contact, Opportunity, Lead Objects you can add more.
            List<List<SObject>> searchList = [FIND :searchText IN ALL FIELDS RETURNING Job__c(Id,Name,Industry__c)];
            //Adding diffrent object's records in sobject list
            for(List<SObject> o:searchList){
                for(SObject s:o){
                    searchedRecord.add(new JobSearch.ResultSet(s));
                }
            }
        }
        return searchedRecord;
    }


This is my Test clas  now:



@isTest(SeeAllData=true)
private class TestJobSearch
{        
private static testMethod void TestJobSearch()
      {
         contact con=new contact(Lastname='A',Email='ryeturi@deloitte.com',FirstName='Srikanth',Phone='9620566619');
         insert con;
        
         List<Job__c> objJob = new List<Job__c>();
        objJob=[Select Job_ID__c,Id,Name,Account__r.Name,CCL_Job_Type__c,Post_Date__c,Industry__c,Job_Category__c,Career_Level__c,AR_Job_Details__c,AR_Job_Application_Link__c from Job__c limit 1];
   
      JobSearch.ResultSet rs = new JobSearch.ResultSet ('a0bR0000005oBDU','Finance Manager');//this is for 2nd constructor
      JobSearch.ResultSet rs1 = new JobSearch.ResultSet (con);
       
       ApexPages.StandardController sc = new ApexPages.StandardController(con);   
       JobSearch createCon = new JobSearch(sc);
             
       PageReference pageRef = Page.jobsearchrepeat;
       Test.setCurrentPage(pageRef);
                 
                   createCon.search();
    }
       }


Regards
Micky