1
public with sharing class ChangePasswordController {
    public String oldPassword {get; set;}
    public String newPassword {get; set;}
    public String verifyNewPassword {get; set;}        
    public PageReference outputText{get;set;}
    public PageReference changePassword() {   
        system.debug('In ChangePasswordController 1 >>>>'); 
        system.debug('oldPassword====' + oldPassword);   
         outputText = Site.changePassword(newPassword, verifyNewPassword, oldPassword); 
         system.debug(outputText);
         try
         {          
            if(outputText != null && outputText.getRedirect()) ***//test case is not covering this loop***
            {
                system.debug('In ChangePasswordController 2 >>>>');
                CommonService commonService = new CommonService();
                CRSessionService sessionService = new CRSessionService(); 
                String userId = commonService.getStandardUserbyID(UserInfo.getUserId()).custUserID__c;   
                system.debug('userId >>>> ' + userId);          
                Users__c user = sessionService.getUserInformationByUserID(userId);  
                system.debug(user);         
                user.isFirstLogin__c = true;
                //user.Password__c = newPassword; 
                UserService userService = new UserService();
                userService.UpdateUser(user);
                return new PageReference('/apex/CRRedirector');
            }
            return outputText;          
         }
        catch(Exception ex)
        {     
            system.debug(ex);       
            ExceptionService.saveException(ex, 'ChangePasswordController', 'ChangePassword', null);
            return null;
        } 
    }     

    public ChangePasswordController() {}   
}

FOr this code I have written test case but its always returning me the null value why its so ?can u please tell me why or you can fix it.

TestClass is --->

@IsTest 
public with sharing class ChangePasswordControllerTest {
    private static User testUser ;
    private static User u2 ;
    static {
        Contact c = [select AccountId , firstName, email, lastname from Contact where FirstName = 'TestUser' limit 1];

        Profile applicantProfile = [Select Id, Name from Profile where Name = 'CashRelease User'];
        testUser = new User(lastname = 'test',firstname = 'test', email = '[email protected]',username= '[email protected]',
        profileId= applicantProfile.id,contactId= c.id,alias = 'test',emailencodingkey='UTF-8',
        languagelocalekey='en_US',localesidkey='en_US', timezonesidkey='America/Los_Angeles');
        insert(testUser);
    }
    @IsTest(SeeAllData=true) public static void testChangePasswordController1() {

        System.setPassword(testUser.Id, 'abc123456');
        ChangePasswordController controller = new ChangePasswordController();
        controller.oldPassword = 'abc123456';
        controller.newPassword = 'qwerty1234'; 
        controller.verifyNewPassword = 'qwerty1234';                
        PageReference retVal = null;

        System.runAs(testUser) {    
            retVal = controller.changePassword();
            System.debug(retVal);
            System.debug('RETURN URL '+Site.getOriginalURL());
            System.assertEquals(retVal, null);  // Everytime its returning null value
        }                      
    }   
}

I am unable to cover if loop lines of code in ChangePassword() method because every time outputtext giving me null value .How can i cover these lines?

4
  • 1
    i like how OutputText is a PageReference Commented Mar 12, 2014 at 9:42
  • Can you show us screenShot from coverage? Or tell which line are not covered? Commented Mar 12, 2014 at 9:54
  • Whole code in the "if(outputText != null && outputText.getRedirect())" loop has not been covered in the try block it is just returning outputtext = null . Commented Mar 12, 2014 at 9:59
  • Why do you have SeeAllData=true? That is best used only as a last resort.
    – Keith C
    Commented Mar 12, 2014 at 10:56

2 Answers 2

2

This Site.changePassword is difficult to test because unless it is called from a configured site it just adds an ApexPages.Message saying it has to be called from a site and returns null which is what your outputText (that is in fact a PageReference) gets set to. And a test isn't a site. It is a pretty poor API from Salesforce.

You will just have to built this behaviour into the test; you can use Test.isRunningTest in your controller to alter the logic to help with that.

To be explicit:

PageReference pr = !Test.isRunningTest()
        ? Site.changePassword(newPassword, verifyNewPassword, oldPassword)
        : new PageReference('fakeChangePasswordResponsePage');
if (pr != null && pr.getRedirect()) {
    // Your logic
} else {
    return pr;
}
6
  • Will you please write the code What you are trying to say ? Commented Mar 12, 2014 at 10:05
  • I've added some code to the answer.
    – Keith C
    Commented Mar 12, 2014 at 10:14
  • Thanks .. i need to ask one thing more what to write in place of new PageReference('fakeChangePasswordResponsePage'); . Should i pass null value here Commented Mar 12, 2014 at 10:22
  • If you wanted to check in your test the specific page reference being returned your test could use pr.getUrl() to check the value and that is the point of putting something like 'fakeChangePasswordResponsePage' in so its clearly something added to simulate some behaviour. It can be anything but I don't think it should be null.
    – Keith C
    Commented Mar 12, 2014 at 10:43
  • I put some page url there . and its working fine .But again the problem is same the logic that i have written is not going to cover in test cases . so how will i cover that logic too to increase the code coverage ? Commented Mar 12, 2014 at 10:48
0

you are not initializing outputText field in your Test Class. So it's null and not going inside the if(outputText != null && outputText.getRedirect()){}

EDIT Check whether it's running by a test class or not in the controller first(This may be a bypass just for the code coverage issue anyway).

outputText = Site.changePassword(newPassword, verifyNewPassword, oldPassword);
if(outputText == null && !Test.isRunningTest())
{
    return outputText;
}
else{
//your logic here
}
2
  • before this loop, outputtext is getting values from Site.ChangePassword(); look above before try block. Commented Mar 12, 2014 at 10:03
  • Doh!It's my bad. Seems like you are getting the same issue where most of developers are facing when testing Site class here see my update
    – highfive
    Commented Mar 12, 2014 at 10:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .