When we are writing Test Class in salesforce, we should follow the following Practice:
- Test class must start with @isTest annotation if class version is more than 25
- Test environment support @testVisible, @testSetUp as well
- The unit test is to test a particular piece of code working properly or not.
- Unit test method takes no argument, commits no data to database, sends no email, flagged with testMethod keyword.
- To deploy to production at-least 75% code coverage is required
- System.debug statement is not counted as a part of the apex code limit.
- Test method and test classes are not counted as a part of code limit
- We should not focus on the percentage of code coverage, we should make sure that every use case should be covered including positive, negative, bulk and single records. Single Action -To verify that the single record produces the correct an expected result.
- Bulk action -Any apex record trigger, class or extension must be invoked for 1-200 records.
- Positive behavior: Test every expected behavior occurs through every expected permutation, i,e user filled out every correct data and not go past the limit.
- Negative Testcase:-Not to add future date, Not to specify a negative amount.
- Restricted User:-Test whether a user with restricted access used in your code.
- Test class should be annotated with @isTest.
- @isTest annotation with test method is equivalent to the testMethod keyword.
- The test method should static and no void return type.
- Test class and method default access are private, no matter to add access specifier.
- Classes with @isTest annotation can't be an interface or enum.
- Test method code can't be invoked by non-test requests.
- Stating with the salesforce API 28.0 test method can not reside inside non-test classes.
- @Testvisible annotation to make visible private methods inside test classes.
- The test method can not be used to test web-service call out. Please use the call out mock.
- You can't send emails from the test method.
- User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
- SeeAllData=true will not work for API 23 version eailer .
- Accessing static resource test records in test class e,g List<Account> accList=Test.loadData(Account,SobjectType,'ResourceName').
- Create a TestFactory class with @isTest annotation to exclude from the organization code size limit.
- @testSetup to create test records once in a method and use in every test method in the test class.
- We can run the unit tests by using Salesforce Standard UI,Force.com IDE, Console, API.
- The maximum number of test classes run per 24 hours of the period is not greater than 500 or 10 multiplication of test classes of your organization.
- As apex runs in system mode so the permission and record sharing are not taken into account. So we need to use the system.runAs to enforce record sharing.
- System.runAs will not enforce user permission or field level permission.
- Every test to runAs count against the total number of DML issued in the process.
Comments
Post a Comment