- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
12-27-2012 12:58 AM
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.