2

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.

5
@IsTest public with sharing class ForgotPasswordControllerTest {
  public static void testForgotPasswordController() {

     Test.StartTest();

    // set the test's page to  VF page (or pass in a PageReference)
    Test.setCurrentPage(Page.yourpage);//replace with our page name

    // 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');  

    List<Apexpages.Message> msgs = ApexPages.getMessages();
    boolean b = false;
    for(Apexpages.Message msg:msgs){
        if (msg.getDetail().contains('A user account for this email address already exists') b = true;
    }
    system.assert(b);//Assert the Page Message was Properly Displayed

    System.assertEquals(controller.forgotPassword(),null); 
  }
}

I have drafted a rough version and that may help.Key is to set current page as your page for which you are testing and assert

  • Thanks a lot Mohith, I have added @IsTest(SeeAllData=true)public static void testForgotPasswordController() { (seeall it's necessary) and also Test.setCurrentPage(Page.ForgotPassword) but the assertion system.assert(b) fail – Enry Aug 16 '13 at 8:35
  • i have also added my custom error message in if (msg.getDetail().contains but it doesn't work – Enry Aug 16 '13 at 8:56
  • Can you check in system logs – Mohith Shrivastava Aug 16 '13 at 8:57
  • In debug log you will be to debug execution – Mohith Shrivastava Aug 16 '13 at 8:57

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.