+ Start a Discussion

system assert

what is the system assert ? what is the system assert equal?

what is the different d/w both of them?

Rahul SharmaRahul Sharma

Asserts are used in apex test classes to validate whether we are getting correct output or not.

Assert statement gets failed if the condition specified is in it is evaluated to be true and thus notifying that the actual and desired values are differed.



Scenario is we are performing some calculation on the field like when discount is inserted as 50%, then the amount of the record must be reflected with the same.

Account a = new Account();
a.Name = 'Test Account';
a.Discount__c = 50;
a.Price__c= 1200;
insert a;

// Now we will check if the discount is reflected in test Account or not using asserts.

Integer updatedPrice = [select Id, Price__c from Account where Id =: a.Id].Price__c;
Integer originalPrice = a.Price__c;

// Now we are adding asserts!
system.assert(updatedPrice == originalPrice);

// Other way of writing it is using assert equals where we just specify 2 parameter(which we need to compare), instead of whole condition
system.assertEquals(updatedPrice, originalPrice);

// system.assertNotEquals is opposite of system.assertEquals
// Note : Both will throw exception when the discount is not reflected in amount, when the test class is ran.



assert -