function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shruthi GM 4Shruthi GM 4 

Test class for a Lightning class:-

/* Class */

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();
        }
    }

}

/* test class */

@istest

public class LightningForgotPasswordControllertest{


public static testmethod void testvalidate(){

String username='shrugm@tcs.com';
String checkEmailUrl='aabgfgb';

test.starttest();
LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
LightningForgotPasswordController.forgotPassowrd('shrugm@tcs.com','aabgfgb');
test.stoptest();
}
}

it is covering 63% of the code.Please help me to cover the entire class!
Ravi Dutt SharmaRavi Dutt Sharma
Hi Shruthi,

Try below class and let me know if it works. This class should give you 100% coverage.
 
@isTest
public class LightningForgotPasswordControllertest{


	public static testmethod void testForgotPasswordWithValidParams(){

		LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
		LightningForgotPasswordController.forgotPassowrd('shrugm@tcs.com','aabgfgb');
	}
	
	public static testmethod void testForgotPasswordWithInvalidParams(){

		LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
		LightningForgotPasswordController.forgotPassowrd(null,null);
	}
	
	public static testmethod void testForgotPasswordWithInvalidEmail(){

		LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
		LightningForgotPasswordController.forgotPassowrd('invalidemail','aabgfgb');
	}
	
}

 
Shruthi GM 4Shruthi GM 4
When I implemented the above code,I got the below errorUser-added image!!!!
Amit Chaudhary 8Amit Chaudhary 8
Please try below code
@istest

public class LightningForgotPasswordControllertest{


	public static testmethod void testvalidate()
	{
		String username='shrugm@tcs.com';
		String checkEmailUrl='aabgfgb';

		test.starttest();
			LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
			LightningForgotPasswordController.forgotPassowrd('shrugm@tcs.com','aabgfgb');

			try
			{				
			LightningForgotPasswordController.forgotPassowrd('shrugm.com','aabgfgb');
			LightningForgotPasswordController.forgotPassowrd(null,null);
			}
			catch(Exception ee)
			{}
		test.stoptest();
	}
	
}
Let us know if this will help you
 
Ravi Dutt SharmaRavi Dutt Sharma
Hey Shruthi,

Its throwing an exception, that is why you are getting this error. Lets put a try catch block around the piece of code when we are passing invalid parameters.
 
@isTest
public class LightningForgotPasswordControllertest{


	public static testmethod void testForgotPasswordWithValidParams(){

		LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
		LightningForgotPasswordController.forgotPassowrd('shrugm@tcs.com','aabgfgb');
	}
	
	public static testmethod void testForgotPasswordWithInvalidParams(){

		LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
		try{
			LightningForgotPasswordController.forgotPassowrd(null,null);
		}catch(Exception e){
			System.debug('Exception: '+e.getMessage());
		}
	}
	
	public static testmethod void testForgotPasswordWithInvalidEmail(){
		
		LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
		try{
			LightningForgotPasswordController.forgotPassowrd('invalidemail','aabgfgb');
		}catch(Exception e){
			System.debug('Exception: '+e.getMessage());
		}
	}
	
}

 
Shruthi GM 4Shruthi GM 4
both the codes are not working....:(..same internal error is coming up!!!
Ravi Dutt SharmaRavi Dutt Sharma
Lets not pass the null values then. Try below code please:
 
@isTest
public class LightningForgotPasswordControllertest{


	public static testmethod void testForgotPasswordWithValidParams(){

		LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
		LightningForgotPasswordController.forgotPassowrd('shrugm@tcs.com','aabgfgb');
	}
	
	
	public static testmethod void testForgotPasswordWithInvalidEmail(){
		
		LightningForgotPasswordController lightningForgot=new LightningForgotPasswordController();
		try{
			LightningForgotPasswordController.forgotPassowrd('invalidemail','aabgfgb');
		}catch(Exception e){
			System.debug('Exception: '+e.getMessage());
		}
	}
	
}

 
Shruthi GM 4Shruthi GM 4
We are not getting error in place of null values!!!! instead we are getting the error in place of "testForgotPasswordWithValidParams" method where we are passing the actual values!!!
Ravi Dutt SharmaRavi Dutt Sharma
Sorry, my bad. In the second parameter you need the pass the Vf page name like '/apex/VFPageName'