I have some problem to cover the following lines with my test class:
if(errormessage=='true'){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'A user account for this email address already exists, would you like to reset your password?');
ApexPages.addMessage(msg);
}
This is my code:
CONTROLLER CLASS:
/**
* An apex page controller that exposes the site forgot password functionality
*/
public with sharing class ForgotPasswordController {
public String username {get; set;}
public ForgotPasswordController() {
//GET HAS ERROR FROM THE BAR
string errormessage=Apexpages.currentpage().getparameters().get('hasError');
if(errormessage=='true'){
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR,'A user account for this email address already exists, would you like to reset your password?');
ApexPages.addMessage(msg);
}
}
public PageReference forgotPassword() {
boolean success = Site.forgotPassword(username);
PageReference pr = Page.ForgotPasswordConfirm;
pr.setRedirect(true);
if (success) {
return pr;
}
return null;
}
}
TEST CLASS:
@IsTest public with sharing class ForgotPasswordControllerTest {
@IsTest(SeeAllData=true) public static void testForgotPasswordController() {
// Instantiate a new controller with all parameters in the page
ForgotPasswordController controller = new ForgotPasswordController();
controller.username = 'test@salesforce.com';
ApexPages.CurrentPage().getParameters().put('hasError','true');
System.assertEquals(controller.forgotPassword(),null);
}
}
Please can you help me to cover those two lines?
I don't know hot to test the ApexPages.Message Error.
Thanks in advantage for any advice
BR.