+ Start a Discussion
SUMIT BANERJEESUMIT BANERJEE 

How to write test class for lightning controller

global class LightningForgotPasswordController {

    public LightningForgotPasswordController() {

    }

    @AuraEnabled
    public static String forgotPassowrd(String username, String checkEmailUrl) {
        try {
            Site.forgotPassword(username);
            ApexPages.PageReference checkEmailRef = new PageReference(checkEmailUrl);
            if(!Site.isValidUsername(username)) {
                return Label.Site.invalid_email;
            }
            aura.redirect(checkEmailRef);
            return null;
        }
        catch (Exception ex) {
            return ex.getMessage();
        }
    }

}
Best Answer chosen by SUMIT BANERJEE
bob_buzzardbob_buzzard
The same way you'd test any other code, by executing the method under test. For example:
 
@isTest
private class LightningForgotPasswordControllerTest()
{
    @isTest
    static void TestForgotPassword()
    {
        String result=LightningForgotPasswordController.forgotPassword('test@test.test', 'http://www.checkemailfake');
        System.assertEquals(null, result);
    }
}

Obviously you'd have to replace 'test@test.test' with an appropriate username associated with your site and you'd want to test against invalid usernames etc, but hopefully you get the idea.