I'm struggling to test a class with pagereference. Following is the class...

public with sharing class redirectOrderPartner{

public String currentRecordId {get;set;}
public orders__c orderId {get;set;}

public redirectOrderPartner(ApexPages.StandardController controller) {

    Public Pagereference go(){

    currentRecordId  = ApexPages.CurrentPage().getparameters().get('orderid');

    system.debug('---currentRecordId  ---'+currentRecordId  );
    orderId = [select id from orders__c where name =: currentRecordId limit 1];


    PageReference pageRef = new PageReference('/apex/OrderTrackingClass?id='+orderId.id);
    return pageRef;



Following is what I tried....

 public class redirectOrderPartner_TEST
          static testMethod void Test(){

          Account acc = new Account(Name='ABC Corp.');
          insert acc;

          orders__c tempOrder = new orders__c(name = '0001234567', account__c = acc.id );
          insert tempOrder;


          PageReference pageRef = Page.redirectOrderPartner;

          ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(tempOrder);
          PW_redirectOrderPartner ec = new redirectOrderPartner(sc);


} I get the following error message on testing..

System.QueryException: List has no rows for assignment to SObject

What am I missing? Thanks


You have a parameter mismatch. In one spot you are looking for 'orderId', and the other you are using 'Id'. I would make the parameter name a constant to facilitate testing.

public with sharing class redirectOrderPartner
    public static final String NAME_PARAM = 'orderId';
    // other stuff

class redirectOrderPartner_TEST
    static testMethod void testRedirect()
        // do some stuff
            redirectOrderPartner.NAME_PARAM, tempOrder.Name
        // do some other stuff
| improve this answer | |
  • Hi @adrian. orderid is a text from external system will append. Based on that I found out the ID of the record, which I will add to the new URL. – MnZ Feb 10 '15 at 17:18
  • Probably my nomenclature is incorrect :) The apex page is working as expected. – MnZ Feb 10 '15 at 17:18

Adrian is right, in your class you get parameter called 'orderid' from Apexpages, but in your test you put parameter called 'Id'. Therefore your method go() doesn't capture the parameter value and in your query you get no results - since the error.

| improve this answer | |
  • 1
    Cees, once you've gained some reputation you'll be able to upvote answers to express agreement. Doing so in another answer may be confusing to some. If you're new to stackexchange, do have a look at our welcome tour – Samuel De Rycke Jul 4 '15 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.