I'm attempting to run a test class for a custom Visualforce controller but am having an issue with my System.AssertEquals method.


public without sharing class CC_LegalController {

public String Brand{get;set;}

public CC_LegalController() {
    hasAcceptedTerms = false;
    User thisUser = [select Id, AccountId, Brand__c from User where Id = :UserInfo.getUserId() ];
    if(thisUser.Brand__c == 'Brand 1'){
        Brand = 'Brand 1';
        Brand = 'Brand 2';

public Boolean hasAcceptedTerms { get; set; }

public PageReference updateUser() {
    update new User(Id=UserInfo.getUserId(), Accepted_Legal_Terms__c=hasAcceptedTerms);
    return Page.CC_Home;

Test Class:

@IsTest global with sharing class CC_LegalController_TC { 
   global static void testCC_LegalController () {
       CC_LegalController controller = new CC_LegalController();
       System.assertEquals(Page.CC_Home, controller.updateUser());       


The error message I am getting is "System.AssertException: Assertion Failed: Expected: System.PageReference[/apex/cc_home], Actual: System.PageReference[/apex/cc_home]" ... which looks equal to me. Any guesses as to what I'm missing? Thanks in advance for your assistance.

1 Answer 1



System.assertEquals(Page.CC_Home.getUrl(), controller.updateUser().getUrl());

should fix it.

The problem is that PageReference doesn't implement value equality (equal when all fields are equal) semantics so only references to the same instance are equal. The getUrl method returns a String which does implement value equality.

As an aside, Page.CC_Home is actually a factory that creates a new PageReference each time. So even:

System.assertEquals(Page.CC_Home, Page.CC_Home);


  • That did it. Thanks not only for the solution but for the clear explanation as well.
    – John_Henry
    Jul 7, 2015 at 13:55
  • @snoeCloud Been bemused by this myself so happy to help.
    – Keith C
    Jul 7, 2015 at 14:02

You must log in to answer this question.

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