+ Start a Discussion
cooldamselcooldamsel 

[Urgent!!!]Test class to cover for loop

Hi,

I want test class to cover my FOR loop.. I am a newbie in salesforce. Kindly help me to do the same..

My apex class looks like the following:

for(i=1; i<3; i++)
{
    if(i==1)
    {
           do something;
    }
    if(i==2)
    {
           do something;
    }
}

Kindly tell me how to test cover this for loop...

Thanks in Advance..

Regards,
Indhu
GlynAGlynA
As written, if your for loop executes at all, then all the code in it will be covered.  Without more detail, I can't provide a better answer.

-Glyn

cooldamselcooldamsel
Apex Class:

public class PerformanceReport
{
public List<PerformanceReportClass> LstPerformanceReportClass {get;set;}

public Class PerformanceReportClass
    {
        public String strMRName{get;set;}
        public String strMonth1{get;set;}
       public List<ReqmtDetailsClass> lstReqmtDetailsClass1{get; set;}
}

public class ReqmtDetailsClass
    {
        public string ReqmtName {get; set;}
        public string ReqmtStatus {get; set;}
        public string ReqmtPostdDate {get; set;}
        public string ResumeReqstd {get; set;}
        public string ResumeSubmtd {get; set;}
        public string ResumeFirstPosted {get; set;}
        public string ElapsedTimeFirstResumePosted {get; set;}
        public string ReqmtClosedDate {get; set;}
        public string ReqmtClosedHrs {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass2 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass3 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass4 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass5 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass6 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass7 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass8 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass9 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass10 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass11 {get; set;}
        public List<CandtDetailsClass> lstCandtdetailClass12 {get; set;}
    }
  
    public class CandtDetailsClass
    {
        public string CandtName{get; set;}
        public string CandtEmail{get; set;}
    }

public PageReference GetReportDetails()
    {
List<User> lstAllUser = new List<User>([SELECT id, name, LastModifiedDate, isActive from user where userroleid in (select id from userrole where name='Management Representative')]);
        List<User> lstUser = new List<User>();
      
        List<RequirementAssignment__c> lstReqmtAssign = new List<RequirementAssignment__c>();
      
        List<RequirementAssignment__c> FilterReqmtAssign ;
      
        List<RequirementAssignment__c> FilterReqmtAssignMonthly ;
      
        List<Requirement__c> lstReqmts = new List<Requirement__c>();

for(User allUser : lstAllUser)
        {
            if(allUser.IsActive == true)
            {
                lstUser.add(allUser);
            }
            else
            {
                if(allUser.LastModifiedDate > startDT)
                {
                    lstUser.add(allUser);
                }
            }
        }
      
      
       lstReqmtAssign = [select Status__c, Requirement__c, Lead_Recruiter__c, Recruiter__c,  createddate, createdbyid from RequirementAssignment__c where createddate >: startDT and createddate <: endDT ];

PerformanceReportClass objPerformanceReportClass = new PerformanceReportClass();
           objPerformanceReportClass.strMonth1 =0;

for(integer i=startMonth; i<=endMonth; i++)
               {
                   FilterReqmtAssignMonthly = new List<RequirementAssignment__c>();
                 
                   for(RequirementAssignment__c  ReqmAssignUser : FilterReqmtAssign)
                   {                   
                       DateTime ReqmtAssignDt = ReqmAssignUser.createddate;
                       Date ReqmAssignDate = Date.NewInstance(ReqmtAssignDt.yeargmt(), ReqmtAssignDt.monthgmt(), ReqmtAssignDt.daygmt());
                                          
                       if((ReqmAssignDate.month() == incrMonth) && (ReqmAssignDate.year() == NewStartDate.year()))
                       {
                           FilterReqmtAssignMonthly.Add(ReqmAssignUser);
                         
                       }
                   }
                       incrMonth++;
                      
System.Debug('List Size:' +FilterReqmtAssignMonthly.size());

              
                       if(i==1)
                       {                         
                           objPerformanceReportClass.strMonth1 = string.valueOf(FilterReqmtAssignMonthly.size());
                          
                            objPerformanceReportClass.lstReqmtDetailsClass1 = new List<ReqmtDetailsClass>();
                          
                            Set<ID> setReqId = new Set<ID>();
                            for(RequirementAssignment__c Reqmt : FilterReqmtAssignMonthly)
                            {
                               
System.Debug('Filter List Monthly'+FilterReqmtAssignMonthly);
                                System.Debug('Monthly Reqmt' +Reqmt);
                                setReqId.add(Reqmt.Requirement__c);                              
                            }
                           
System.Debug('Id Set:' +setReqId);
                            if(setReqId.size()>0)
                            {                              
                                for(ID ReqID : setReqId)
                                {                        
                                   
System.Debug('Reqmt Id:' + ReqID);        
                                    List<Requirement__c> lstReqmtsResume= [Select id, Name, Status__c, createddate, No_Of_Resumes__c, Resumes_Submitted__c, Assign_Candidate_date__c, First_Response__c, Req_Closed_Date__c, Req_Closed_hrs__c  from Requirement__c where id =:ReqID];
                                   
System.Debug('list Reqmts Resume:' +lstReqmtsResume);
                                  
                                    List<Candidate_Mapping__c> lstCandtMapping = [select Candidate__r.Candidate_Full_Name__c , Candidate__r.Email__c from Candidate_Mapping__c where Requirement__c =:ReqID];
                                   
System.Debug('Candidates List:' +lstCandtMapping);
                                  
                                    if(lstReqmtsResume.size()>0)
                                    {
                                        for(Requirement__c req:lstReqmtsResume)
                                        {
                                            ReqmtDetailsClass objReqmtDetailsClass1 = new ReqmtDetailsClass();
                                            objReqmtDetailsClass1.lstCandtdetailClass = new List<CandtDetailsClass>();
                                          
                                           
System.Debug('Reqmt Resume:' +req);
                                             objReqmtDetailsClass1.ReqmtName = string.valueOf(req.Name);  
                                             objReqmtDetailsClass1.ReqmtStatus = string.valueOf(req.Status__c);
                                             objReqmtDetailsClass1.ReqmtPostdDate = string.valueOf(req.createddate);
                                             objReqmtDetailsClass1.ResumeReqstd = string.valueOf(req.No_Of_Resumes__c);
                                             objReqmtDetailsClass1.ResumeSubmtd = string.valueOf(req.Resumes_Submitted__c);
                                             objReqmtDetailsClass1.ResumeFirstPosted = string.valueOf(req.Assign_Candidate_date__c);
                                             objReqmtDetailsClass1.ElapsedTimeFirstResumePosted = string.valueOf(req.First_Response__c);
                                             objReqmtDetailsClass1.ReqmtClosedDate = string.valueOf(req.Req_Closed_Date__c);
                                             objReqmtDetailsClass1.ReqmtClosedHrs = string.valueOf(req.Req_Closed_hrs__c);
                                           
                                            
System.Debug('objReqmtDetailsClass1:'+objReqmtDetailsClass1);
                                             objPerformanceReportClass.lstReqmtDetailsClass1.add(objReqmtDetailsClass1);
                                          
                                             for(Candidate_Mapping__c candt :lstCandtMapping)
                                             {
                                                 CandtDetailsClass objCandtDetailsClass1 = new CandtDetailsClass();
                                               
                                                 objCandtDetailsClass1.CandtName = string.valueOf(candt.Candidate__r.Candidate_Full_Name__c);
                                                 objCandtDetailsClass1.CandtEmail = string.valueOf(candt.Candidate__r.Email__c);
                                                 objReqmtDetailsClass1.lstCandtdetailClass.add(objCandtDetailsClass1);

                                             }
                                        }
                                    }                                 
                                }
                              
                            }

                       }
         }
}

The bold lines within my For loop are not test covered.
cooldamselcooldamsel
My Test Class is :

@isTest
private class testclassperformancereport
{
   static testmethod void myTestclass()
   {
     
      datetime nowdate = datetime.now();
     
      datetime newdate= nowdate.addMonths(2);
     
      PerformanceReport pr = new PerformanceReport();
     
      pr.strFromDate = nowdate.format('MM/dd/yyyy hh:mm a');  
      pr.strToDate = newdate.format('MM/dd/yyyy hh:mm a');
     
      PerformanceReport.PerformanceReportClass prc = new PerformanceReport.PerformanceReportClass();
     
      list<PerformanceReportClass> c = new List<PerformanceReportClass>();
     
      PerformanceReport.ReqmtDetailsClass rdc = new PerformanceReport.ReqmtDetailsClass();
     
      PerformanceReport.CandtDetailsClass cdc = new PerformanceReport.CandtDetailsClass();
      
      User mrActUser = [SELECT id, name, LastModifiedDate from user where userroleid in (select id from userrole where name='Management Representative') and isActive = True limit 1];
     
      List<RequirementAssignment__c> lstReqmtAssign = new List<RequirementAssignment__c>();
     
      List<Requirement__c> lstReqmts = new List<Requirement__c>();
    
      datetime startdate = datetime.parse(pr.strFromDate);
      system.debug('startdate :' + startdate);
      
      datetime enddate = datetime.parse(pr.strToDate);
      system.debug('enddate :' + enddate);
     
      System.test.startTest();
     
      Requirement__c reqmc=new Requirement__c();
           
      reqmc.Name='SampathReq';
      reqmc.Job_Title__c='.net';     
      reqmc.Duration__c='6';
      reqmc.No_Of_Resumes__c=2;
      reqmc.Min_Cost__c=100;
      reqmc.Max_Cost__c=200;
      reqmc.Rate__c=200;
      reqmc.Rate_Basis__c='Hourly';
      reqmc.Status__c='open';
      reqmc.Position_Type__c='Contract';
      reqmc.State__c='CA';
      reqmc.City__c='CA';
      reqmc.Est_Start_Date__c=date.today();
               
      insert reqmc;
     
      Candidate__c candc=new Candidate__c();
                       
      candc.Name='test';
      candc.Last_name__c='testonek';
      candc.Current_city__c='chennai';
      candc.Current_state__c='Maine';
      candc.Gender__c='Male';
      candc.Employer_Name__c='ttt';
      candc.Email__c='test@gmail.com';
      candc.interview_contact_phone__c='Mobile Phone';
      candc.Contract_type__c='Corp to Corp';
      candc.visa_type__c='H1 B';
      candc.cost__c =100;
      candc.mobile_phone_no__c='9890043500';
      candc.employer_contact_email__c='test@gmail.com';
      candc.Employer_Contact_Name_Name__c='tt';
      candc.Employer_Mobile_No__c='9988009876';      
      candc.Employer_Name__c = 'jos';
      candc.Employer_Work_Phone_No__c = '(425) 264-6771';             
      candc.Followup_Date__c =date.today();   

      insert candc;
     
      Candidate_Mapping__c candmapc = new Candidate_Mapping__c();

      candmapc.Candidate__c=candc.id;     
     
      RequirementAssignment__c reqassc = new RequirementAssignment__c();
       
      reqassc.Requirement__c = reqmc.id;
      reqassc.AssignStatus__c= 'true';
      reqassc.Won__c= 'no';
  
      insert reqassc;
               
      candmapc.Requirement__c=reqmc.id;
      candmapc.Employer_s_Authorization__c = 'test';
      candmapc.status1__c = 'Applied'; 
      candmapc.LR_Comments__c = 'test by lr';
      candmapc.MR_Comments__c = 'test by MR';
      candmapc.Requirement_Owner_Email__c = 'testOwner@preludesys.com';
      candmapc.Manager_Email_ID__c = 'testOwner1@preludesys.com';
      candmapc.LR_Status__c = 'Approved';
      candmapc.MR_Status__c = 'Approved';
      candmapc.LR_Status_Date__c = string.valueOf(date.today());             
      candmapc.Submitted_to_Client__c = 'Yes';
      candmapc.Interview_Scheduled__c = 'Yes';
      candmapc.Interview_Accepted__c = 'Yes';
      candmapc.Client_Interviewed__c = 'Yes';
      candmapc.Client_Offered__c = 'Yes';
      candmapc.Comments__c = ''; 
      candmapc.MR_Status1__c = 'Approved' ;

      insert candmapc;
     
      pr.GetReportDetails();
     
    
   }
}