Salesforce Testing, Testing in Salesforce Apex

Salesforce Testing or Testing in Apex 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 the application will run for a long time.

There are two ways of testing an application.

  1. One is through the Salesforce user interfaces (testing with a single record)
  2.  The other way is to test for building functionality, i.e.Testing with bulk records by using an 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. 

  • 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 tests.

  1. Calls to system debug are not counted as part of apex code coverage.
  2. When we are 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.
  3. When we are 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.

  1.  Single records.
  2. Bulk records.
  3. Positive scenarios.
  4. Negative scenarios.
  5. Restricted user
  • Single Records: – This includes testing to verify that a single record produces the correct, expected result.
  • 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 bulky cases as well.
  • Positive scenarios: – This type of component testing expects a system to save a record without error.
  • Negative scenarios: – This type of component testing expects a system to give an error.
  • 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.

Types of Testing in Salesforce Apex.

  1. Unit Test: Testing each component individually is called unit testing. It is a job of the developer.
  2. Integration Testing: This is the testing which is combination of unit testing.
  3. System testing: Once the integration testing completes, system testing is the testing after rectifying some of the errors in the integration testing.