Unit Testing in Salesforce
There are few things to consider before you deploy or upload the code or package;
- 75% of your Apex code must be covered by unit tests
- All the tests must complete successfully
- Every trigger has some test coverage (1%)
- All classes and triggers must compile successfully
- Single Action :Test to verify that a single record produces the correct, expected result.
- Bulk Action : Test not only the single record case, but the bulk cases as well
- Positive Behavior : Verify that the expected behavior occurs through every expected permutation
- Negative Behavior : Verify that the error messages are correctly produced
- Restricted User :Test whether a user with restricted access to the sObjects used in your code sees the expected behavior
Test Class can be defined @isTest annotation. Before the Winter 12′ release we had only private test classes, but on Winter 12′ release salesforce has given the chance to write public test classes as well. Salesforce has released the public test classes for expose common methods for data creation. It can be used to setting up data that the tests need to run against. Public test methods can be called from a running test but not from a non-test request.
- Use static
- Use testMethod keyword
- Use void return type
- No any arguments
- No data changes performed in a test method
- Don’t send emails
- Cannot be used to test Web service callout because web services are asynchronous and tests are synchronous.
- The API for asynchronous test runs is a Beta release (Winter ’12)
- For More : Force.com Apex code Developer’s Guide Page 153