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
debradebra 

Apex test method with AssertEquals fails when values are equal?

I have a simple apex controller extension and created a basic unit test to verify the returned page reference from the controller is expected value.
When I run the test I get failure with below message - clearing showing the values do match???

System.AssertException: Assertion Failed: Page Reference should be for the Opportunity:
Expected: System.PageReference[/0065B000007l82WQAQ],
Actual: System.PageReference[/0065B000007l82WQAQ]

Here is test code:
     @isTest
     static void testOppContractCreateNotCreated() {   /* opportunity stage is not set to correct value for contract create */
        setupTestData();
        PageReference expectRef = new PageReference('/' + opp.Id);
          Test.startTest();
                stdCont = new ApexPages.Standardcontroller(opp);
            contExt = new OpportunityCreateContractContExt(stdCont);  
             PageReference createContractPageRef = Page.createAdSalesContract;
            Test.setCurrentPage(createContractPageRef);
            PageReference goToRef = contExt.getgoToContract();
            system.debug(opp);
            system.debug(goToRef);            
         Test.stopTest();
        system.assertEquals(expectRef, goToRef,'Page Reference should be for the Opportunity');
     }
Best Answer chosen by debra
Suraj PSuraj P
You're comparing 2 different instances of a non-primitive object. You should not expect them to be equal. Try this:
system.assertEquals(expectRef.getUrl(), goToRef.getUrl(),'Page Reference should be for the Opportunity');

 

All Answers

Suraj PSuraj P
You're comparing 2 different instances of a non-primitive object. You should not expect them to be equal. Try this:
system.assertEquals(expectRef.getUrl(), goToRef.getUrl(),'Page Reference should be for the Opportunity');

 
This was selected as the best answer
debradebra
Thanks that did the trick!