5

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

Controller:

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';
    }else{
        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 { 
   @IsTest(SeeAllData=true) 
   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.

5

This:

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

fails.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.