Understanding Testing in Apex
Understanding Testing in Apex:
Testing is the key to successful long term development and is a critical component of the development process. And testing is key to the success of application particularly if application to be deployed to customers.
If we validate that application works as expected, that there are no unexpected behaviors, then application will run for longtime.
There are two ways of testing an application.
- One is through the salesforce user interfaces (testing with a single record)
- The other way is to test for build functionality i.e testing with bulk records by using apex data loader up to 200 records can be passed through code.
- Before we can deploy code or package into production, the following must be true.
Check out the top Salesforce Interview Questions now!
→ 75 % of apex code must be covered by unit tests. All of those tests must complete successfully.
→ When deploying to a production organization we need to have 75% of apex code covered by tests.
Note: Our focus should not be on the percentage of code that is covered instead we make sure that every use of application is covered including positive and negative cases, as well as bulk and single record. This should lead to 75% or more of code being covered by unit test.
- Calls to system debug are not counted as part of apex code coverage.
- When we deploying apex code from one sandbox to another sandbox then we no need of any apex code coverage, i.e no need to write any test classes.
- When we deploying apex code from one sandbox to production organization then we need to have at least 75% of code coverage, i.e we need to write test classes with minimum code coverage of 75%
What to Test In Apex:
Salesforce.com recommends the following components need to test.
- Single records
- Bulk records
- Positive scenarios
- Negative scenarios
- Restricted user
1. Single Records:
This includes testing to verify that a single record produces the correct, expected result
2. Bulk Records:-
Any apex code, whether a triggers, a class or on extension may be used for 1 to 200 records we must test not only the single record case, but the bulk cases as well.
3. Positive scenarios:-
This type of component testing expect a system to save a record without error.
4. Negative scenarios:-
This type of component testing expect a system to give error.
5. Restricted User:-
Test whether a user with restricted access to the objects used in code sees the expected behavior, i.e whether they can run the code or receive error messages.
For indepth understanding of Salesforce click on