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

Test coverage for method 'replaceFirst'
Hi All,
I am trying for test coverage for one class as below
Class::
Public class sample123{
public sample123(){
}
public PageReference PostBtn(){
// Workaround for getting the current page name
String pageName = ApexPages.CurrentPage().getUrl();
pageName = pageName.replaceFirst('/apex/','');
pageName = EncodingUtil.urlEncode(pageName, 'UTF-8');
string[] pageNameExtra = pageName.split('%3F',0);
pageName = pageNameExtra[0];
pagereference pr = new pagereference('/apex/sample?prevpg='+pageName);
pr.setredirect(true);
return pr;
}
}
Test class::
Public class sampletest{
public static testmethod void sample123Method(){
sample123 smp = new sample123();
apexpages.currentpage().getparameters().put('/','/apex/');
smp.PostBtn();
}
}
I am getting this error 'System.NullPointerException: Attempt to de-reference a null object' at the execution of this statement
pageName = pageName.replaceFirst('/apex/','');
Please give me solution for this test coverage, thanks in advance
I am trying for test coverage for one class as below
Class::
Public class sample123{
public sample123(){
}
public PageReference PostBtn(){
// Workaround for getting the current page name
String pageName = ApexPages.CurrentPage().getUrl();
pageName = pageName.replaceFirst('/apex/','');
pageName = EncodingUtil.urlEncode(pageName, 'UTF-8');
string[] pageNameExtra = pageName.split('%3F',0);
pageName = pageNameExtra[0];
pagereference pr = new pagereference('/apex/sample?prevpg='+pageName);
pr.setredirect(true);
return pr;
}
}
Test class::
Public class sampletest{
public static testmethod void sample123Method(){
sample123 smp = new sample123();
apexpages.currentpage().getparameters().put('/','/apex/');
smp.PostBtn();
}
}
I am getting this error 'System.NullPointerException: Attempt to de-reference a null object' at the execution of this statement
pageName = pageName.replaceFirst('/apex/','');
Please give me solution for this test coverage, thanks in advance
in the test method you have to set the current visualforce page in this way: Whil this line is wrong:
This should solve your problem.
Hope this helps
--
May the Force.com be with you!
All Answers
in the test method you have to set the current visualforce page in this way: Whil this line is wrong:
This should solve your problem.
Hope this helps
--
May the Force.com be with you!