0

My Business class (Custom Controller)

public with sharing class SalesTask {

    public PageReference clientApproved() {
        //Update the current task to close
        pmt.Status__c = 'Completed';
        pmt.Type_of_layout__c = 'Support';
        pmt.Proposal_Status__c = 'Client Approved';
        update pmt;

        PageReference cancel= new PageReference(System.currentPageReference().getParameters().get('retURL'));
        cancel.setRedirect(true);
        return cancel;
    }
  public SalesTask(){
        this.pmt = new PMO_Task__c();
        List<PMO_Task__c> pmtList = new List<PMO_Task__c>();
        pmtList = [SELECT Subject__c,AssigedDate__c,AssignedBy__c,MyComments__c,Status__c,Opportunity__c,Type_of_layout__c FROM PMO_Task__c WHERE id = :System.currentPageReference().getParameters().get('id') ];

        pmt = pmtList.get(0);

        }

Test Class which I have written

@isTest
public class PSalesTaskTest{

    private static testmethod  void SalesTaskTest() {
       PageReference pg = Page.pmotrackertasks;
      Test.setCurrentPage(pg);
      pg.getParameters().put('Status__c', 'Completed');
      pg.getParameters().put('Type_of_layout__c', 'Support');
      pg.getParameters().put('Proposal_Status__c', 'Client Approved');

      PMOSalesTask pmo = new PMOSalesTask();


      pmo.reject();

    }
}

Kindly help me to achieve code coverage i'm not able to call method from test class to i'm getting error when run the test class

System.ListException: List index out of bounds: 0

1
  • Unfortunately the information that you have provided does not seem to be sufficient. In your test class you have created an instance of PMOSalesTask however the controller which you have shared is SalesTask (and not PMOSalesTask!) which does not have any reject method. Apr 15, 2015 at 14:20

2 Answers 2

1

Per the comment above, check if this is the class that you wanted to share.

If this is it, then this must be your problem. You have instantiated your list when you are assigning your SOQL query result to it. But then you are assuming that the query returns data and you are accessing the first element.

So do this:

pmtList = [SELECT Subject__c,AssigedDate__c,AssignedBy__c,MyComments__c,Status__c,Opportunity__c,Type_of_layout__c FROM PMO_Task__c WHERE id = :System.currentPageReference().getParameters().get('id') ];
if(pmtList.size() > 0)
{
    pmt = pmtList.get(0);
}

So you will need to check this list size before accessing the list element.

Also if you can modify your logic, its better to use a for loop to iterate through the list.

1

The reason you are getting the list out of bounds error is that your testclass/method doesn't mock any data.

Your controller's SOQL:

pmtList = [SELECT Subject__c,AssigedDate__c,AssignedBy__c,MyComments__c,Status__c,Opportunity__c,Type_of_layout__c 
      FROM PMO_Task__c 
      WHERE id = :System.currentPageReference().getParameters().get('id') ];

will return null for pmtList because your test method didn't create any PMO_Task__c objects.

Testmethods don't see any org data (except for setup data like User) if they are at version 24 or higher. Consult the apex doc on mocking up test data. there are lots of techniques for this. Once the data is mocked in the testemthod, you can invoke your controller's constructor and then each action method, getter, and setter to get code coverage and verify behavior.

@RichardN also is correct in handling SOQL where the result is no rows found.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .