Salesforce provides a framework for testing apex code. Testing is the key to successful software development. Before deploying apex code, try to cover as many lines of code as you can. At least 75% of your Apex code must be covered by unit tests else your code will not be deployed to production.
You should not write test classes by keeping in my mind just to cover 75% of your code but should always try to cover your entire code before deploying to production.
- Test provides assurance of functionality
- Test reduce cost of change
- Test encourage modular and reusable code
- Test help document expected behavior
- Test + Code = less likely produce the bug
What to Test?
- Things that should happen to happen (Positive) : Positive testing is a way to test expected behaviour of code that you wrote is suppose to do.
- Things that should not happen to happen (Negative) : This is the hardest and important thing to test that verify if the error messages are correctly produced along with the positive working of test cases, within the limits cases.
- User Access : This is basically the manual testing, which we perform by seeing the aspect of end-user test is done when we think of user interaction. This type of test is done by hand via insert, update, delete and undelete the records.
- Exceptions : Exception testing tests expected behaviour of code when an user with restricted access to sObjects are used in code.
- Bulk Testing : This test comes in when a class, trigger or any apex code insert and update data in bulk. We need to test the class by creating more than one record in multiple scenarios.
Principles for Best Practices
Principle #1 Use of Asserts :
Assert is the method of System Class. It is an important method for test class which is used to check the expected result of test scenario.
“A test without assert method isn’t a test, it’s code execution.”
There are three in-built assert methods which are as follows:
- assert(condition, message)
- assertEquals(expected, actual, message)
- assertNotEquals(expected, actual, message)
Every test method should include at least one assertion.
Use startTest() and stopTest()
startTest() methods are used to reset governor limits. The code between Test.startTest and Test.stopTest executes in fresh set of governor limits that means your setup code will not interfere and give you false positives (or negatives) surrounding governor limits.
stopTest() method focuses on asynchronous code to complete.
In general, one should follow these steps while writing test class:
- Create your test data
- Start the test
- Use that test data within your tested method
- Stop the test
- Assert your code work as expected
Generate Test Data
Use ‘@TestSetup’ annotation to generate Test data. @TestSetup annotation always ensures following:
- Checks if right data exists in your test.
- Can reduce the execution time
- Test Setup method executes first before execution of any method of test class.
You can also setup your test data in util classes which helps you to use it elsewhere too.
Use of runAs Method
System.runAs method is used to test user access. This method is helpful to verify that user can not do things for which they do not have permission to do.
The runAs method do not enforce user permissions or field-level permissions but helps only in record sharing. Also, it ignores user license limit also.
In addition to ensuring the quality of your code, unit tests enable you to meet the code coverage requirements for deploying or packaging Apex. To deploy Apex or package it for the Force.com AppExchange, unit tests must cover at least 75% of your Apex code, and those tests must pass.