Apex

Best Practices for Writing Test Class in Salesforce

Best Practices for Writing Test Class in Salesforce

Apart from the rule that we all are aware of having atleast 75% code coverage , here are the list of best practices which should be followed while writing a Test Class in Salesforce.

  • Prepare Test Data:
    • Instead of relying on the organizations data create your own test Data.
    • Create all test data before calling the Test.startTest method.
    • Test Data Deletion is not required as test classes uses test data for simulations and dont commit them.
  • Create separate test classes for all classes and triggers. Never test your entire application in a single test.
  • Make sure your test covers all the branches of conditional Statements.
  • Make calls to methods using both valid and invalid inputs.This is often referred as positive and Negative Test..
  • Always handle all exceptions that are caught, instead of merely catching the exceptions.
  • Use System.assert methods to check the output is as per the our assumptions.
  • Use the runAs method to test your application in different user contexts.
  • Exercise bulk trigger functionality—use at least 100 records in your tests.
  • Use the ORDER BY keywords to ensure that the records are returned in the expected order.
Feel Free to Comment and Add points!! 

About Saurabh Dua

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Powered by Blogger.