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
Abdul RazzaqAbdul Razzaq 

Writing Test Class for pagereference method

public class DispatcherContactNewController {

    public DispatcherContactNewController(ApexPages.StandardController controller) {
        this.controller = controller;
    }
   
    public PageReference Redir() {
PageReference newPage;
    PageReference standardpage = New PageReference('/00Q/e?retURL=%2F00Q%2Fo&RecordType=012900000011wvV&ent=Lead&nooverride=1');
        RecordType r;
        r = [Select id,developerName from RecordType Where SobjectType='Lead' And id = :ApexPages.currentPage().getParameters().get('RecordType')];
        
        if ( r.developerName == 'LeadVisualforcePage') {
        
        System.debug('----------------------------------------------------------------------');
        
            newPage = Page.leadtask;
            newPage.setRedirect(true);
            return newPage;
        } else {
        
         System.debug('**********************************************************************');
         
         return standardpage;
        }

    }

    private final ApexPages.StandardController controller;
}
Best Answer chosen by Abdul Razzaq
Pankaj_GanwaniPankaj_Ganwani
Hi,

You can supply the developername in page parameters and then create the instance of the class and call the pagereference method. I have done it in the sample code mentioned in my first comment.

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hi,

I think your standard controller is lead object.

@isTest
private class TestClass
{
       static testMethod void check()
       {
                   
                   Lead objLead = new Lead(Name = 'Test');//please give value of all required fields
                   insert objLead;
                   
                   ApexPages.currentPage().getParameters().put('RecordType','LeadVisualforcePage');
                  ApexPages.Standardcontroller sc = new ApexPages.Standardcontroller(objLead);
                  DispatcherContactNewController obj = new DispatcherContactNewController (sc);
                  obj.Redir();
       }
}
Abdul RazzaqAbdul Razzaq
Hi pankaj,

I think inserting values and new lead record would not be important for my test class, However tried that.
But the error still lies on the same part 
getting the recordtype id,

"   r = [Select id,developerName from RecordType Where SobjectType='Lead' And id = :ApexPages.currentPage().getParameters().get('RecordType')];"

this part is were I have been facing problem. 
I get the similar error while I run test. 

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

stack trace:-
Class.DispatcherContactNewController.Redir: line 13, column 1
Class.testeg.testDispatcherContactNewController: line 12, column 1
Pankaj_GanwaniPankaj_Ganwani
Hi,

Sorry, please use this SOQL for fetching the record type:

r = [Select id,developerName from RecordType Where SobjectType='Lead' And DeveloperName= :ApexPages.currentPage().getParameters().get('RecordType')];
 
Abdul RazzaqAbdul Razzaq
how do I use this recordtype for implementing in testclass
Abdul RazzaqAbdul Razzaq
I am trying this at the end.
system.assertequals(r.developername, 'LeadvisualforcePage');
I guess,
I even have to initialize the vfpage that runs inside the if loop.. 
Pankaj_GanwaniPankaj_Ganwani
Hi,

You can supply the developername in page parameters and then create the instance of the class and call the pagereference method. I have done it in the sample code mentioned in my first comment.
This was selected as the best answer