+ Start a Discussion
Shrey TyagiShrey Tyagi 

Set variables in test class - Please help!!

Hi Everyone ,
         I have an apex class whose constructor is calling a method . The call out is done after setting 1 parameter . The code is given below:

Constructor:

  public ProposalSearchControllerModified() {
     //String variable for soql
      soql = 'Select Id,Name from Opportunity where name != null';
     //executing SOQL AND PAASING VALUES TO THE LIST.
     Opportunities = Database.query(soql + ' order by ' + sortField + ' ' + sortDir+ ' Limit 50');
     //USING 1ST RECORD OF LIST TO EXECUTE ANOTHE METHOD - CLEAR Order
     DetailRecordId=Opportunities[0].Id;    
    ClearOrder();
  }


Test Class Code:

@istest
public class ProposalSearchControllerModifiedTest{
    public static testmethod void ProposalSearchMethod(){
         
         
         PageReference pageRef = Page.ProposalSearchModified;
         pageRef.getParameters().put('Name', 'Te');
         pageRef.getParameters().put('OpportunityID', '0');
         pageRef.getParameters().put('SolicitationNo', '0');
         pageRef.getParameters().put('ProposalNo', '0');
         
         Test.setCurrentPage(pageRef);
         ProposalSearchControllerModified cs=new ProposalSearchControllerModified();
         String sd=cs.sortDir;
         String sf=cs.sortField;
         String ds=cs.debugSoql;
         list<String> ls=cs.Stages;
         list<String> ls1=cs.SubStages;
         list<String> ls2=cs.FiscalYearValues;
         
         cs.toggleSort();
         cs.runQuery();
         cs.runSearch();
         cs.reset();
         
         
    
    }
}​



Error Message:

System.ListException: List index out of bounds: 0

Error is thrown when highlighted line in code above is covered through test class.
Best Answer chosen by Shrey Tyagi
Sure@DreamSure@Dream
Hi Shrey

Looks like there is no Opportunity record. 

In test classes, we need to create our own test data.
 

All Answers

Sure@DreamSure@Dream
Hi Shrey

Looks like there is no Opportunity record. 

In test classes, we need to create our own test data.
 
This was selected as the best answer
Shrey TyagiShrey Tyagi
Oops . Can't believe I missed that . It works . Thanks Dude!!!!