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 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');
it is mostly used in test method for testing some condition with equality.
And throws the exception , if equals check is incorrect.
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’);
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