You need to sign in to do that
Don't have an account?

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;
}
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;
}
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
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();
}
}
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
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')];
system.assertequals(r.developername, 'LeadvisualforcePage');
I guess,
I even have to initialize the vfpage that runs inside the if loop..
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.