4

I have a pagereference for which I want to write a test class but get the error Expected: System.PageReference[/CustomLoginPage], Actual: System.PageReference[/CustomLoginPage]. What am I doing wrong:

Class

    /**
 * An apex page controller that exposes the site login functionality
 */
global with sharing class CommunitiesLoginController {

    global CommunitiesLoginController () {}

    // Code we will invoke on page load.
    global PageReference forwardToAuthPage() {
        String startUrl = System.currentPageReference().getParameters().get('startURL');
        String displayType = System.currentPageReference().getParameters().get('display');
        return Network.forwardToAuthPage(startUrl, displayType);
    }
    // Code we will invoke on page load.
    global PageReference forwardToCustomAuthPage() {
        return new PageReference('/CustomLoginPage');
    }
}

Testclass

    /**
 * An apex page controller that exposes the site login functionality
 */
@IsTest global with sharing class CommunitiesLoginControllerTest {
    @IsTest(SeeAllData=true) 
    global static void testCommunitiesLoginController () {
        CommunitiesLoginController controller = new CommunitiesLoginController();
        System.assertEquals(null, controller.forwardToAuthPage());  
        PageReference p = new PageReference ('/CustomLoginPage');
        System.assertEquals( p, controller.forwardToCustomAuthPage());   
    } 
}
0

3 Answers 3

4

I tend to use .getUrl() and .startsWith() e.g.

System.assert(nextPage.getUrl().startsWith(Page.ExpectedPage.getUrl()), nextPage.getUrl());

It's good to use startsWith() in case the controller puts parameters on the end.

4

When you compare 2 objects in Salesforce (as far as it is known it is based on Java), it actually compares not objects, but their hashCode results, or what is returned as compareTo method from Comparable interface. Since given class (PageReference) is system, it is impossible to know if two PageReference objects are same - event if those are created from same URL, there might be some differences inside since those.

The proper approach would be to compare a result of getUrl() method.

For example:

@IsTest global with sharing class CommunitiesLoginControllerTest {
    @IsTest(SeeAllData=true) 
    global static void testCommunitiesLoginController () {
        CommunitiesLoginController controller = new CommunitiesLoginController();
        System.assertEquals(null, controller.forwardToAuthPage());  
        PageReference p = new PageReference ('/CustomLoginPage');
        String expectedUrl = p.getUrl();
        PageReference actualPageReference = controller.forwardToCustomAuthPage();
        System.assertNotEquals(null, actualPageReference);
        System.assertNotEquals(expectedUrl, actualPageReference.getUrl());

    } 
}

It might be also wise to check other properties.

2
  • 1
    Great answer, but a minor nitpick: .hashCode() is not used for equality checks in this object (afaik), but appears to directly use .equals(Object). However, this method is private/protected and cannot be accessed by user code. Instead, it seems to perform a memory-equality check (===), such that the assertion can only succeed if it is literally the same PageReference object (e.g. PageReference p1 = new PageReference(...); PageReference p2 = p1;)
    – sfdcfox
    Apr 29, 2019 at 14:13
  • Thanks for the comment, I will improve an answer!
    – kurunve
    Apr 29, 2019 at 16:21
3

You could use getUrl() method to compare the URLs in an assert:

System.assertEquals(p.getUrl(), controller.forwardToCustomAuthPage().getUrl());

Also note that the use ofSeeAllData=true in test class is a bad practice.

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