2

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];

    system.debug('---orderid---'+orderId);

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

  }

 }

Following is what I tried....

  @isTest
 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;

          Test.startTest();

          PageReference pageRef = Page.redirectOrderPartner;
          Test.setCurrentPage(pageRef);

          ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(tempOrder);
          ApexPages.currentPage().getParameters().put('Id',tempOrder.name);
          PW_redirectOrderPartner ec = new redirectOrderPartner(sc);
          ec.go();                        
          Test.stopTest();


    }

} I get the following error message on testing..

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

What am I missing? Thanks

1

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
}

@IsTest
class redirectOrderPartner_TEST
{
    static testMethod void testRedirect()
    {
        // do some stuff
        ApexPages.currentPage().getParameters().put(
            redirectOrderPartner.NAME_PARAM, tempOrder.Name
        );
        // do some other stuff
    }
}
  • 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
-1

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.

  • 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.