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
Soumya Srivastava 1Soumya Srivastava 1 

Error in test class having list out of bound

Hi All,
I was trying to create test class of one of my apex class. Can you help me out in writing the same as I am facing difficulties in writing the same. In this Application__c is the custom object with fields as there in the query. In which outcome fields are picklist fields.

Test Class
@isTest
public class Update_InterviewDetailsTest{
    static integer batchsiz = 80; 
    static List < Application__c > testApplication = new List < Application__c > ();
    
    public static List<Date> X1st_Interview_Date = new List<Date>();
    public static List<Date> X2nd_Interview_Date = new List<Date>();
    public static List<Date> X3rd_Interview_Date = new List<Date>();
    public static List<string> X1st_Interview_Time = new List<string>();
    public static List<string> X2nd_Interview_Time = new List<string>();
..................................................
    
 static void setUp()
    {
        createApplicationRecords();
    } 
 static void createApplicationRecords(){
       Application__c ap = new Application__c();
         for(integer i=0;i<batchsiz;i++){         
     
                ap.X1st_Interview_Date__c = X1st_Interview_Date[i];
                ap.X1st_Interview_Time__c = X1st_Interview_Time[i];
                ap.X1st_Interview_Outcome__c = X1st_Interview_Outcome[i];
                ap.X2nd_Interview_Date__c = X2nd_Interview_Date[i];         
              .............................................................
         }
       Test.startTest();
   {         
        insert ap;
   }
  Test.stopTest();        
       PageReference pageRef = Page.Update_Interview;
       Test.setCurrentPage(pageRef);         
        
        ApexPages.StandardController sc = new ApexPages.StandardController(ap);
        UpdateInterviewDetails uids = new UpdateInterviewDetails(sc); 
        String nextPage = uids.save().getUrl();
        // Verify that page fails without parameters
        System.assertEquals('/apex/failure?error=noParam', nextPage);
       
        // Add parameters to page URL            
        ApexPages.currentPage().getParameters().put('Id',String.valueOf(ap.id)); 
        String aId = ApexPages.currentPage().getParameters().get('id');          
        uids.waitapplication();
        uids.save();         
            
  }
  
public static testmethod void myunittest(){
    
 setup();

  System.AssertEquals(testApplication.size(), 80, 'Incorrect number of Test Applicaiton Created.');
    
    for(Integer i=0;i<batchsiz;i++){
        System.assertEquals(testApplication[i].X1st_Interview_Outcome__c,X1st_Interview_Outcome[i]);
        System.assertEquals(testApplication[i].X2nd_Interview_Outcome__c,X2nd_Interview_Outcome[i]);
        System.assertEquals(testApplication[i].X3rd_Interview_Outcome__c,X3rd_Interview_Outcome[i]);  
        }
  }     
}
Nayana KNayana K
Can you please post controller and VF page code?
Soumya Srivastava 1Soumya Srivastava 1
Apex Class(In this class I am getting error at waitapplication() called in constructor and where waitapplication is used when i run the test class.Also, it was not covering refreshpage and stdController along with save().Currently coverage is 46%.)

public class UpdateInterviewDetails{ 
//Public Properties
public Application__c app {get;set;}
    public Boolean refreshPage {public get; private set;}
    public ApexPages.StandardController stdController { get; set; }
           
        //Constructor, After loading the page to display india pageblocksection by default
    public UpdateInterviewDetails(ApexPages.StandardController controller) {
      this.stdController = controller;    
      waitapplication();
      refreshPage = true;      
    }
 
   public Application__c waitapplication(){
    app = [Select Id,Submission_Outcome__c,X1st_Interview_Date__c,X1st_Interview_Outcome__c,X1st_Interview_Time__c,X2nd_Interview_Date__c,X2nd_Interview_Outcome__c,X2nd_Interview_Time__c,X3rd_Interview_Date__c,X3rd_Interview_Time__c,X3rd_Interview_Outcome__c From Application__c WHERE Id = :ApexPages.currentPage().getParameters().get('id')];    
   return app;
   }
    //After changing picklist value based on the selection to display 
   public PageReference save(){
       System.Debug('app contains:  ' + ApexPages.currentPage().getParameters().get('id'));              
       update app;   
     //  refreshPage=true;
            
   return stdController.save();
   }
   
}

VF Page:
<apex:form id="form">
<apex:pageBlock id="myBlock"> <apex:outputPanel rendered="{!refreshPage}"> <script> window.center.location='/{!Application__c.id}'; </script> </apex:outputPanel>
<!--renderd to display or hide the pageblocksection on any other element of VF page-->
<apex:pageBlockSection id="pbS0">
<apex:inputField id="sl1" onchange="pickListcall();" rendered="{!(app.Submission_Outcome__c != null && app.Submission_Outcome__c != '')}" value="{!app.Submission_Outcome__c}"/>
</apex:pageBlockSection>
<table class="tableClass" border="0" cellpadding="10" cellspacing="5"> <colgroup span="2"></colgroup> <thead> <tr>
<div id="abc" align="center" style="display: none;" padding="10">
<apex:outputLabel id="out1" for="field1" value="Interview1 Date"></apex:outputLabel>
<apex:inputField id="field1" value="{!app.X1st_Interview_Date__c}"/>
<apex:outputLabel id="out2" for="field2" value="Interview1 Time"></apex:outputLabel>
<apex:inputField id="field2" value="{!app.X1st_Interview_Time__c}"/>
<apex:outputLabel id="out3" for="field3" value="Interview1 Outcome"></apex:outputLabel>
<apex:inputField id="field3" onchange="pickOutcome1();" value="{!app.X1st_Interview_Outcome__c}"/>
</div> </tr>
<tr>
<div id="abc1" align="center" position="relative" style="display: none;">
<apex:outputLabel id="out4" for="field4" value="Interview2 Date"></apex:outputLabel>
<apex:inputField id="field4" value="{!app.X2nd_Interview_Date__c}"/>
<apex:outputLabel id="out5" for="field5" value="Interview2 Time">
</apex:outputLabel> <apex:inputField id="field5" value="{!app.X2nd_Interview_Time__c}" />
<apex:outputLabel id="out6" for="field6" value="Interview2 Outcome"></apex:outputLabel>
<apex:inputField id="field6" onChange="pickOutcome2();" value="{!app.X2nd_Interview_Outcome__c}"/>
</div>
</tr> <tr padding="10">
<div id="abc2" align="center" position="relative" style="display: none;">
<apex:outputLabel id="out7" for="field4" value="Interview3 Date"></apex:outputLabel> <apex:inputField id="field7" value="{!app.X3rd_Interview_Date__c}"/> <apex:outputLabel id="out8" for="field5" value="Interview3 Time"></apex:outputLabel>
<apex:inputField id="field8" value="{!app.X3rd_Interview_Time__c}"/> <apex:outputLabel id="out9" for="field5" value="Interview3 Outcome"></apex:outputLabel>
<apex:inputField id="field9" value="{!app.X3rd_Interview_Outcome__c}"/> </div> </tr> </thead> <tfoot> <tr> <td colspan="2" scope="colgroup"> <div align="center">
<apex:commandLink action="{!save}" target="_parent" value="Save" styleClass="btn" style="text-decoration:none;padding:4px;"/>
</div> </td> </tr> </tfoot> </table> </apex:pageBlock>
</apex:form> </apex:page>
Soumya Srivastava 1Soumya Srivastava 1
Hi @nayana

Can you please reply if possible.

Thanks,
Soumya
Nayana KNayana K
@isTest
public class Update_InterviewDetailsTest{

 static testMethod void updateAppTest(){
		// inserting new application 
        Application__c ap = new Application__c();
		ap.X1st_Interview_Date__c = Date.Today().addDays(3);
		ap.X1st_Interview_Time__c = 'PUT SOME VALID VALUE HERE';
		ap.X1st_Interview_Outcome__c = 'PUT SOME VALID VALUE HERE';
		// repeat these for X2nd and X3rd fields
        
		// insert application 
        insert ap;
  
       PageReference pageRef = Page.Update_Interview;
       Test.setCurrentPage(pageRef);         
        ApexPages.currentPage().getParameters().put('Id',String.valueOf(ap.id));
        ApexPages.StandardController sc = new ApexPages.StandardController(ap);
        UpdateInterviewDetails uids = new UpdateInterviewDetails(sc);
		
		system.assertEquals(true, uids.refreshPage);
		
		uids.app.X1st_Interview_Date__c = Date.Today().addDays(7);
        uids.save();        
		system.assertEquals(Date.Today().addDays(7), [SELECT X1st_Interview_Date__c FROM Application__c WHERE Id =: ap.Id].X1st_Interview_Date__c);
            
  }
}

Let me know if you have any doubts in test class

 
Soumya Srivastava 1Soumya Srivastava 1
Hi Nayana,

I am a beginner in apex though trying to develop my skills in apex. Thanks, for your help.  Problem was with the line in apex class as :
   if(ida != null){
    app = [Select Id,Submission_Outcome__c,X1st_Interview_Date__c,X1st_Interview_Outcome__c,X1st_Interview_Time__c,X2nd_Interview_Date__c,X2nd_Interview_Outcome__c,X2nd_Interview_Time__c,X3rd_Interview_Date__c,X3rd_Interview_Time__c,X3rd_Interview_Outcome__c From Application__c WHERE Id = :ida];    
   }
Currently, the catch exception is not covered in the test class. I want to cover it and want to add more assertion in the test class to test it.

If you have any idea then please do let me know.

Thanks,
Soumya
Soumya Srivastava 1Soumya Srivastava 1
Hi,

I also want to add bulk testing in this , if you have any idea like this how can i proceed.

public static List<Application__c> testApplication = new  List<Application__c>();
    
        System.AssertEquals(testApplication.size(), 80, 'Incorrect number of Test Applicaiton Created.');    
      for(Integer i=0;i<batchsiz;i++){
        System.assertEquals(testApplication[i].X1st_Interview_Outcome__c,X1st_Interview_Outcome[i]);
        System.assertEquals(testApplication[i].X2nd_Interview_Outcome__c,X2nd_Interview_Outcome[i]);
        System.assertEquals(testApplication[i].X3rd_Interview_Outcome__c,X3rd_Interview_Outcome[i]);  
        }

Thanks,
Soumya
Soumya Srivastava 1Soumya Srivastava 1
Hi,

How can i give coverage to this line. And when I add this line which is below, it gives me an error .
uids.app.X1st_Interview_Date__c = Date.Today().addDays(7);

Please let me know when you  have time.

Thanks,
Soumya
Nayana KNayana K
Hi Soumya,

Bulk testing is needed in case of triggers or batches.

For this VF page bulk testing is not good idea. 

If you are referring my code, can you please post whole code which you are trying to save and where the error is. Also please let me know the field data type of X1st_Interview_Date__c, X1st_Interview_Time__c, X1st_Interview_Outcome__c.
Soumya Srivastava 1Soumya Srivastava 1
@isTest
public class Update_InterviewDetailsTest{

public static testmethod void myunittest(){    

       Application__c e = new Application__c();     
 // prepare data   
       e.X1st_Interview_Date__c= Date.Today().addDays(3);
       e.X1st_Interview_Time__c='12.45';
       e.X1st_Interview_Outcome__c='2nd Interview ';
       e.Date_Submitted__c = Date.Today().addDays(3);
       e.Submission_Outcome__c = '1st Interview';
       e.X2nd_Interview_Date__c= Date.Today().addDays(3);
       e.X2nd_Interview_Time__c='00.00';
       e.X2nd_Interview_Outcome__c='Awaiting Client feedback';
       e.X3rd_Interview_Date__c= Date.Today().addDays(3);
       e.X3rd_Interview_Time__c='19.00';
       e.X3rd_Interview_Outcome__c='No Show'; 
     
     if(e.X1st_Interview_Outcome__c == '2nd Interview'){   
        System.assertEquals( '2nd Interview',e.X1st_Interview_Outcome__c, 'Interview should be a 2nd Interview.');
     }
     if(e.X2nd_Interview_Outcome__c == '3rd Interview'){   
      System.assertEquals( '3rd Interview',e.X2nd_Interview_Outcome__c, 'Interview should be a 3rd Interview.');
     }       
   Test.startTest();
   {   
     if(Test.isRunningTest()) {
         try{
             insert e;  
            system.assertEquals(Date.Today().addDays(3), [SELECT X1st_Interview_Date__c FROM Application__c WHERE Id =: e.Id].X1st_Interview_Date__c);
            system.assertEquals(Date.Today().addDays(3), [SELECT X2nd_Interview_Date__c FROM Application__c WHERE Id =: e.Id].X2nd_Interview_Date__c);
            system.assertEquals(Date.Today().addDays(3), [SELECT X3rd_Interview_Date__c FROM Application__c WHERE Id =: e.Id].X3rd_Interview_Date__c);
         } 
         catch(Exception ex){
          System.assert(ex.getMessage().contains('expected message'), 'message=' + ex.getMessage());     
         }
     }
       PageReference pageRef = Page.Update_Interview;
       Test.setCurrentPage(pageRef);    
       ApexPages.StandardController sc = new ApexPages.StandardController(e);
       UpdateInterviewDetails uids = new UpdateInterviewDetails(sc);
       system.assertNotEquals(true, uids.refreshPage);
      uids.app.X1st_Interview_Date__c = Date.Today().addDays(7);    // 

       // Add parameters to page URL            
       ApexPages.currentPage().getParameters().put('Id',string.valueOf(e.id));       
       uids.waitapplication(e.id);       
       uids.save();     
     }
  Test.stopTest();        
  }     
}

System.NullPointerException: Attempt to de-reference a null object on line 44    uids.app.X1st_Interview_...... Also I am not able to cover excetion which I have done in try catch block in test.startTest and test.stopTest
 
Soumya Srivastava 1Soumya Srivastava 1
Datatype for Fields are Date,Text and Picklist of  X1st_Interview_Date__c, X1st_Interview_Time__c, X1st_Interview_Outcome__c respectively.
Nayana KNayana K
Replace :
PageReference pageRef = Page.Update_Interview;
       Test.setCurrentPage(pageRef);    
       ApexPages.StandardController sc = new ApexPages.StandardController(e);
       UpdateInterviewDetails uids = new UpdateInterviewDetails(sc);
       system.assertNotEquals(true, uids.refreshPage);
      uids.app.X1st_Interview_Date__c = Date.Today().addDays(7);    // 

       // Add parameters to page URL            
       ApexPages.currentPage().getParameters().put('Id',string.valueOf(e.id));       
       uids.waitapplication(e.id);       
       uids.save();
with :
PageReference pageRef = Page.Update_Interview;
       Test.setCurrentPage(pageRef);   
		// Add parameters to page URL            
       ApexPages.currentPage().getParameters().put('Id',string.valueOf(e.id));    	   
       ApexPages.StandardController sc = new ApexPages.StandardController(e);
       UpdateInterviewDetails uids = new UpdateInterviewDetails(sc);
       system.assertNotEquals(true, uids.refreshPage);
       uids.app.X1st_Interview_Date__c = Date.Today().addDays(7);    // 
       uids.waitapplication(e.id);       
       uids.save();

Also I din't get the code piece you are trying to cover exception. I din't understand how it will cover your VF code. It has no relation with your VF.
Nayana KNayana K
@isTest
public class Update_InterviewDetailsTest{

 static testMethod void updateAppTest(){
		// inserting new application 
        Application__c ap = new Application__c();
		ap.X1st_Interview_Date__c= Date.Today().addDays(3);
       ap.X1st_Interview_Time__c='12.45';
       ap.X1st_Interview_Outcome__c='2nd Interview ';
       ap.Date_Submitted__c = Date.Today().addDays(3);
       ap.Submission_Outcome__c = '1st Interview';
       ap.X2nd_Interview_Date__c= Date.Today().addDays(3);
       ap.X2nd_Interview_Time__c='00.00';
       ap.X2nd_Interview_Outcome__c='Awaiting Client feedback';
       ap.X3rd_Interview_Date__c= Date.Today().addDays(3);
       ap.X3rd_Interview_Time__c='19.00';
       ap.X3rd_Interview_Outcome__c='No Show'; 
        
		// insert application 
        insert ap;
  
       PageReference pageRef = Page.Update_Interview;
       Test.setCurrentPage(pageRef);         
        ApexPages.currentPage().getParameters().put('Id',String.valueOf(ap.id));
        ApexPages.StandardController sc = new ApexPages.StandardController(ap);
        UpdateInterviewDetails uids = new UpdateInterviewDetails(sc);
		
		system.assertEquals(true, uids.refreshPage);
		
		uids.app.X1st_Interview_Date__c = Date.Today().addDays(7);
        uids.save();        
		
		// check if after save() called, app updated properly with correct X1st_Interview_Date__c value
		system.assertEquals(Date.Today().addDays(7), [SELECT X1st_Interview_Date__c FROM Application__c WHERE Id =: ap.Id].X1st_Interview_Date__c);
            
  }
}

Can you please try this exact code?
Soumya Srivastava 1Soumya Srivastava 1
Hi,

My assert fails at refresh page assertion. Meanwhile i have checked for this and it returns value--
 System.debug('Checking updated  X1st_Interview_Date__c value :'+ uids.app.X1st_Interview_Date__c);

Shall I add more assertion with System.AssertEquals for 2nd and 3rd Interview also like for line.
system.assertEquals(Date.Today().addDays(7), [SELECT X1st_Interview_Date__c FROMApplication__c WHERE Id =: ap.Id].X1st_Interview_Date__c);

VF page is embedded on application custom object. It will show validation exception which are there on custom object also along with validations applicaple on VF page like on outcome picklist with 2nd interview. So, how shall give coverage to exception in my test class. If you have any idea.

Thanks,
Soumya
Soumya Srivastava 1Soumya Srivastava 1
Also , currently assertion also fails for 

  System.assertEquals(Date.Today().addDays(7), [SELECT X1st_Interview_Date__c FROM Application__c WHERE Id =: e.Id].X1st_Interview_Date__c);        
It gives this error :
System.AssertException: Assertion Failed: Expected: 2016-08-02 00:00:00, Actual: 2016-07-29 00:00:00
Soumya Srivastava 1Soumya Srivastava 1
User-added image
Soumya Srivastava 1Soumya Srivastava 1
Hi, 

I have one urgent query regarding VF page redirecting to Validation Error after save button is clicked. It should redirect to other page after save when validation are performed.

Please let me know if you have any idea. How can i make validations to appear on same page or on custom object of same record page.

Thanks,
Soumya Srivastava
Soumya Srivastava 1Soumya Srivastava 1
Hi,

I need to add certain code in Save page refrence, now I need to have a coverage on this 
    //After changing picklist value based on the selection to display 
   public PageReference save(){
   try{     
       if(app.X1st_Interview_Outcome__c != '2nd Interview'){
           app.X2nd_Interview_Date__c = NULL;
           app.X2nd_Interview_Time__c = ''; 
         update app; 
       }
        if(app.X2nd_Interview_Outcome__c != '3rd Interview'){
           app.X3rd_Interview_Date__c = NULL;
           app.X3rd_Interview_Time__c = '';
         update app; 
       }
       update app;
         refreshPage=true;  
      return stdController.save();
   }

Shall I just add the assertion for the same.

Thanks,
Soumya
Soumya Srivastava 1Soumya Srivastava 1
HI,
I was trying to add this assertion though where I want a null value when outcome is not equal to 3rd Interview or 2nd Interview.
But it says failed when i use assertEquals therefore I tried with NotEquals but coverage has gone down currently as I have added some code in apex class.
   
 if(e.X1st_Interview_Outcome__c != '2nd Interview'){      
         System.assertNotEquals(NULL,e.X2nd_Interview_Date__c, 'Interview Date should have null value.');
         System.assertNotEquals('',e.X2nd_Interview_Time__c, 'Interview Time should have null value.');
    }
     if(e.X2nd_Interview_Outcome__c != '3rd Interview'){   
         System.assertNotEquals(NULL,e.X3rd_Interview_Date__c, 'Interview Date should have null value.');
         System.assertNotEquals('',e.X3rd_Interview_Time__c, 'Interview Time should have null value.');
     }  

Here is the method in class :
 //After changing picklist value based on the selection to display 
   public PageReference save(){
   try{     
       if(app.X1st_Interview_Outcome__c != '2nd Interview'){
           app.X2nd_Interview_Date__c = NULL;
           app.X2nd_Interview_Time__c = ''; 
         update app; 
       }
        if(app.X2nd_Interview_Outcome__c != '3rd Interview'){
           app.X3rd_Interview_Date__c = NULL;
           app.X3rd_Interview_Time__c = '';
         update app; 
       }
       update app;
         refreshPage=true;  
      return stdController.save();
   }
 
Soumya Srivastava 1Soumya Srivastava 1
Hi Nayana,

Can you tell me how can i cover if else condition in a test class.

Thanks,
Soumya