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
Omega3kOmega3k 

Unit Test in Apex Code Help

Hi,

 

The following code throws a nullPointerException at the red highlighted line.

 

/**
 * An apex page controller that exposes the change password functionality
 */
public class ChangePasswordController {
    public String oldPassword {get; set;}
    public String newPassword {get; set;}
    public String verifyNewPassword {get; set;}        
    
    public PageReference changePassword() {
        //return Site.changePassword(newPassword, verifyNewPassword, oldpassword); 
        Site.changePassword(newPassword, verifyNewPassword, oldpassword);    
        return new PageReference(Site.getOriginalUrl());     }     
    
    public ChangePasswordController() {}
    
    public static testMethod void testChangePasswordController() {
        // Instantiate a new controller with all parameters in the page
        ChangePasswordController controller = new ChangePasswordController();
        controller.oldPassword = '123456';
        controller.newPassword = 'qwerty1'; 
        controller.verifyNewPassword = 'qwerty1';                
        
        System.assertEquals(controller.changePassword(),null);                        
    }    
}

 

As it is, the code coverage is at 100% (with the error)... I tried fixing the error by checking the "oldPassword", "newPassword", and "verifyNewPassword" individually, but then the code coverage drops to 57% (the blue highlighted section doesn't get checked). This is not ideal... Can anyone give me some guidance as to how I can fix the error and keep the code coverage at 100%?

 

Thanks

MiddhaMiddha

Add a check in your  "changePassword" method:

 

 

public PageReference changePassword() 
    {
        //return Site.changePassword(newPassword, verifyNewPassword, oldpassword); 
        Site.changePassword(newPassword, verifyNewPassword, oldpassword);    
        String s  = Site.getOriginalUrl();
        if(s!=null)
        	return new PageReference(s);
        else 
        	return null;	
             
        } 

 

 

Omega3kOmega3k

Thanks a lot! That did the trick, although code coverage dropped to 90%...

 

Thanks again.