To Unit Test or Not to Unit Test

To Unit Test or Not to Unit TestAny Apex developer worth his or her salt knows the requirements for deploying Apex code to Production: at least 75% of the Apex code in Production must be covered, and every trigger must be touched. These are the minimum requirements. But are you satisfied with meeting the bare minimum requirements? Are there times when unit testing is a good idea, even if those test go over and above the minimum?

Consider this scenario. You’re a Salesforce administrator and developer, working in IT. You’ve been asked to develop a complex process to allow Sales Management to review and approve certain types of Opportunities. The project involves validation rules, complicated workflow rules, a thorny approval process, and absolutely no Apex. A week later, you’ve finished the job in the sandbox, and you’re ready to have it tested before you deploy to Production?

Should you write unit tests?

Technically, of course, you don’t have to unit test this process. There’s no Apex involved, so when you deploy to Production, any existing unit tests won’t even be run. The 75% rule doesn’t even apply. So why would you write unit tests? There are several reasons.

  • First, unit tests are a great way to verify that your validation rules work properly. Do your validation rules catch every possible data entry error? Do they allow every possible combination of valid data to be entered? Testing this manually is tedious, and if you make one little change to a validation rule, you may forget to test all the ramifications of that change. Unit tests allow you to automate that testing.
  • Second, unit tests are a great way to ensure that most your workflow works properly. Does the rule fire when you expect it to? Does your workflow field update work properly? Unit tests can’t verify everything about workflow (like whether email alerts or outbound messages are sent properly), but they help you verify at least part of your workflow; manual testing can verify the rest.
  • Third, unit tests are a great way to test the approval process. When a record is submitted for approval, does it show up on the right people’s queues? When a record is approved or rejected, is the record updated properly? Unit tests are a great way to verify the flow of your approval process.
  • Fourth, unit tests are a great way to test the end-to-end process while running as users with different profiles. As System Administrators, we usually test under our own Salesforce login, but often neglect to test as a non-admin user. With unit tests (using the System.runAs() method), we can write code that tests the process while running as users with a variety of profiles. Doing this testing manually can be tedious; with Apex, you can automate the tedium.
  • Fifth, unit tests are a great way to ensure that, when the process that works fine in the sandbox is deployed to Production, it still works properly. How many times have you deployed to Production, but forgotten to activate workflow, or forgotten to deploy an important validation rule? Unit tests are a great way to ensure that what works in the sandbox also works in Production.

With apologies to William Shakespeare:

To unit test, or not to unit test: that is the question:
Whether ‘tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by unit testing, end them?

MJ Kahn, SVP of Technology at OpFocus

about the author

MJ Kahn

At OpFocus, MJ architects and constructs solutions that would impress the builders of the pyramids. She solves technical puzzles that would frustrate daVinci. She leaps tall buildings (like the new Salesforce tower) in a single bound.

Well ok, maybe she doesn’t. But she does help lead an amazing team of smart, talented, and dedicated consultants who do. MJ’s job at OpFocus is provide technical leadership and guidance to OpFocus clients and team members so that, working together, we can create innovative yet practical solutions to real-world business problems within the Salesforce ecosystem.

Prior to OpFocus, MJ built an extensive background in technology and has held a variety of development, consulting, and management positions at companies like Sybase, Vignette, and Honeywell, among others. In these roles, MJ’s focus was helping companies effectively and intelligently use technology to solve business problems. An Apex and Visualforce consultant since mid-2008, MJ has worked with scores of companies to help them understand and utilize platforms like to solve business issues and also guide them in developing their own AppExchange product offerings.