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
Raj88Raj88 

Help needed for covering Test class

Class:


public class Cls_getSchemesPendingApproval{
  
     Public class wrapperClass{
       public Boolean checked{get; set;}
       public ID approvalProcessID{get; set;}
       public ID schemesRecordID{get; set;}
       public String payname{get; set;}
       public String schemedesc{get; set;}
       public String shortname {get; set;}
       public String period{get; set;}
       public String deal_id{get; set;}
       public String status{get; set;}
       public String DateStart {get; set;}
       public String schemetype{get; set;}
       public String DateEnd {get; set;}
    //   public boolean held{get; set;}
       //public ERS_schemess__c schemes{get; set;}
       public wrapperClass(){}   
   }
   public String stringCustomer {get;set;}
   Set<ID> paySet = new Set<ID>();
   Map<Id, Id> processMap = new Map<Id, Id>();
   public List<wrapperClass> wrapClassList{get; set;}
   public List<wrapperClass> wrapClassHeldList{get; set;}
   public List <ERS_schemes__c> payList {get;set;}
  
   public List<wrapperClass> wrapList{get; set;}
   //Number of records to be shown in each
  
  
   public Integer intPageSize {get; set;}
   //Total number of records in the search results list
   public Integer intRecordSize {get; set;}
   public ApexPages.StandardSetController con{ get; set;}
   //Total number of records in the search results list
   public integer intTotalRecords{get; set;}
  

  
   //returns the results list to be displayed on the screen
   public List<wrapperClass> testList{
    get{
        List<wrapperClass> lstWrap = new List<wrapperClass>();
      if(wrapList!=null && wrapList.size()>0){
        List<ERS_schemes__c> lstschemes = con.getRecords();
        For(ERS_schemes__c obj :  lstschemes){
            for(wrapperClass ow : wraplist){
                if(ow.schemesRecordID == obj.id){
                    lstWrap.add(ow);
                }
            }
        }
        return lstWrap;
      }else{
        return null;
      }
    }
    set;
  }
  
  
   public pageReference doAction(){
        wrapList = getschemes();     
        setResultsInController(wrapList);
        return null;
   }
  
     /*
  * This method sets the result list in the controller along with other properties like
  * page size. Used for pagination
  */
  private void setResultsInController(List<wrapperClass> wrapList){
    try{
      intPageSize = 1;
      Set<String> schemesRecordIds = new Set<string>();
      for(wrapperClass obj : wrapList){
        schemesRecordIds.add(obj.schemesRecordID);
      }
     
      con  =  new ApexPages.StandardSetController([Select id from ERS_schemes__c where id in :schemesRecordIds order by createdDate desc]);
      con.setPageSize(intPageSize);
      integer intResultSize = con.getResultSize();
      if(math.mod(intResultSize,intPageSize) == 0)
        intRecordSize = (con.getResultSize())/intPageSize;
      else
        intRecordSize = ((con.getResultSize())/intPageSize) +1;
      intTotalRecords=wrapList.size();
    }catch(Exception e){
     
    }
  }
  
  
   public List<wrapperClass> getschemes(){
   List<Approval.ProcessWorkitemRequest> PWRList  = new List<Approval.ProcessWorkitemRequest>();
   wrapClassList = new List <wrapperClass>();
   wrapClassHeldList = new List <wrapperClass>();
   // get the queues which the user belongs to
   List<GroupMember> queues = [SELECT GroupId FROM GroupMember where UserOrGroupId  = :userInfo.getUserID()];
   List<String> users = new List<String>();
   users.add(userInfo.getUserID());
   //add the queues to which the user belongs
   for( GroupMember grp : queues){
       users.add(grp.GroupId);
   }
  
  // List<ProcessInstanceWorkitem> piwiList = [SELECT ActorId,Id,IsDeleted,ProcessInstanceId, ProcessInstance.targetobjectid
    //      FROM ProcessInstanceWorkitem where actorid = :userInfo.getUserID()];
   
    List<ProcessInstanceWorkitem> piwiList = [SELECT ActorId,Id,IsDeleted,ProcessInstanceId, ProcessInstance.targetobjectid
          FROM ProcessInstanceWorkitem where actorid in :users];
         
  
   if(!piwiList.isempty()){  
         // create a set of the schemes ids
               
       for (ProcessInstanceWorkitem piwi : piwiList){
               if(String.valueof(piwi.ProcessInstance.targetobjectid.getSobjectType())== 'ERS_schemes__c'){
                   paySet.add(piwi.ProcessInstance.targetobjectid);
                   processMap.put(piwi.ProcessInstance.targetobjectid, piwi.id);
                   //system.debug('vikkk' + piwi.ProcessInstance.targetobjectid);
                   //system.debug('hiya' + piwi.id);
               }
       }
     
            payList = [select  id, EndDate__c,  SchemeShortName__c, StartDate__c, Status_desc__c, SchemeType__c from ERS_schemes__c where id in
                   :paySet];
    
      
       
        for(ERS_schemes__c pay : payList){
                wrapperClass wrapClass = new wrapperClass();
                wrapClass.checked = false;
                wrapClass.approvalProcessID = processMap.get(pay.id);
              
                wrapClass.schemesRecordID = pay.id;
                //wrapClass.schemedesc = pay.SchemeDescription__c;
                wrapClass.shortname = pay.SchemeShortName__c;
                wrapClass.status = pay.Status_desc__c;
                wrapClass.DateEnd = pay.EndDate__c.day() + '-' + pay.EndDate__c.month() + '-' + pay.EndDate__c.year();
                wrapClass.schemetype=  pay.SchemeType__c ;
                wrapClass.DateStart = pay.StartDate__c.day() + '-' + pay.StartDate__c.month() + '-' + pay.StartDate__c.year();
                wrapClassList.add(wrapClass);
          }   
         }

    
   return wrapClassList;        
   } 
  }



Test Class:

@isTest
private class Test_getSchemesPendingApproval
{
static testMethod void testTrigger()
{
Cls_getSchemesPendingApproval pdd= new Cls_getSchemesPendingApproval();
Cls_getSchemesPendingApproval.wrapperclass wrapperclass=new Cls_getSchemesPendingApproval.wrapperclass();
List<Cls_getSchemesPendingApproval.wrapperclass> wrapperclass1=new List<Cls_getSchemesPendingApproval.wrapperclass>();
wrapperclass1.add(wrapperclass);

List<ERS_Schemes__c> scheme = new List<ERS_Schemes__c>();

  ERS_Schemes__c scheme1 = new ERS_Schemes__c();
    scheme1.SchemeShortName__c='te';
    scheme1.SchemeDescription__c='test';
    scheme1.EndDate__c=Date.newInstance(2014,12,02);
    scheme1.StartDate__c=Date.newInstance(2014,12,01);
    //scheme.Status__c='ACT';
    insert scheme1;
    scheme.add(scheme1);

Test.StartTest();
pdd.paylist=scheme;
pdd.wrapClassHeldList=wrapperclass1;
pdd.wrapClassList=wrapperclass1;
pdd.testList=wrapperclass1;
pdd.doAction();
pdd.getschemes();
ApexPages.StandardSetController sc = new ApexPages.StandardSetController(scheme);
pdd.con = sc;
Test.StopTest();
}
}


Getting only 66%. Can anyone help me covering 100%...Lines in BOLD are not covered.
Ashish_SFDCAshish_SFDC

Hi Raj, 


The test class has to Instantiate the Objects in the Apex Class Code. 

You have to Insert Dummy records in your test class which fall into the Catagory for those For Loops and execute every method in the For Loop. 


Regards,

Ashish

Abhi_TripathiAbhi_Tripathi
Hi Raj,

Go for this post, hope this helps
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

Regards,
Abhi Tripathi