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
LionLion 

Reg: system.assertEquals()

Hi,

 

What is the purpose of System.assertEquals().

Where we use this method exactly?

 

 

Thanks & Regards:

 

 

CLKCLK

it is mostly used in test method for testing some condition with equality.

And throws the exception , if equals check is incorrect.

Pradeep_NavatarPradeep_Navatar

System.assertEquals() is used to validate two values are equal. Basically it is used in test method. This method asserts that the first two arguments, x and y are the same, if they are not a runtime exception is thrown.

 Example:

 

Account ac=new Account(name=’testName’);

Insert ac;

Account acc=[select id,name from account where id=:ac.id];

System.assertEquals(acc.name, ‘testName’);

SEKAR RAJ.SEKAR RAJ.
Hi Lion,

System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false.
System.assertEquals(expected, actual, msg);

For Example :
Opportunity opp = new Opportunity();
opp.StageName = 'Sourcing Demand';
opp.AccountId = acc.id;
opp.CloseDate = System.today();
opp.Name = 'test -ExPay';
insert opp;

// Query the inserted opportunity record
List<Opportunity> oppList = [SELECT id,Name,Stage FROM Opportunity Where Id=: opp.Id];

Here, Consider the scenario that After inserting an opportunity, the trigger will change the opportunity Stage as "Prospecting".

System.assertEquals(ExpectedResult,ActualResult); // original syntax
System.assertEquals('Prospecting','oppList[0].Stage'); 

Thanks,
SEKAR RAJ
Esteban SalazarEsteban Salazar
System.assertEquals() validate that two values are equal, if they are not the same a runtime exception is throws