Automated testing confirms whether a piece of particular code is working correctly. And, its no secret, that writing automated tests helps decrease bugs and locate existing bugs.
Why write test methods?
- Test Methods are required to deploy Apex to a production environment. In Salesforce, all classes must have at least 75% code coverage.
- If you are releasing an app onto Salesforce’s Force.com AppExchange they are required.
- Test methods provide test automation which can enable greater QA efficiency, and can help avoid creating future bugs in the maintenance stage.
- They provide a very easy way of doing automated regression testing.
Within the development community there are a lot of debates about how much testing is required.
Types of Tests
Unit Tests test the least lines of code; but reduce external dependencies by using mock objects.
Unit Tests can be really challenging to implement in really interconnected code bases.
Integration Testing covers how the different “units” interact together.
Some Best Practices
- Don’t Depend on Pre-Existing Data
- use System.runAs() methods to verify test users can find records that were shared with apex
- Mimic VisualForce page interactions by explicity calling methods
Author: Brian Cline