Testing is in imperative aspect of the software development life-cycle. In this episode we will learn about writing unit test code to verify the implemented business logic using Apex. Join us as you embark on this wonderful journey to become a champion Salesforce developer.
- What are Unit Tests?
- Why are Unit Tests needed?
- Anatomy of a Unit Test Code
- Running Unit Tests and understanding code coverage
- Q & A
Most importantly don’t break a leg if you are overwhelmed with the pace of the live sessions. All Apex Hours for Student sessions will be recorded and will be available on our YouTube channel.
Please register here and follow our ApexHours website to get more notification.
Here is some keynote of session
What are Unit tests
- In other word, we can say that unit tests are the test performed by the developers to ensure that functionality is working as expected considering Both positive & Negative tests.
- Unit tests are also used in long-term development project to ensure that project is running error-free & smooth.
Why Unit tests are needed
- Validate Desired Behavior
- Reduce the bug Cost
- Perform Bulk tests
- 75% Code coverage for Production deployment
What to Test
- Apex Trigger
- Apex Class – Handler/Helper, WebService, Apex REST, SOAP
- VF Page
- Apex Batch/Queueable/Future Method
- Custom Controller
I have written unit test, now what ?
Once you have created/developed the Unit test for your Apex Class or Apex Trigger. Now, it’s time to run the tests
1) Using Developer Console
2) Using Salesforce Interface
TestSetup method in Test Class
- Use testsetup method in test class to create the data at once and use it across all the test methods in the same class.
- Increase performance of the test class.
- Testsetup methods executes first before any testmethod executes in test class.
- If any test method, modified the record like field update or deletion, these kinds of changes rolled back after finish of the testmethod and next method gets the original unmodified record.
- If a class contains testSetup method then you can not use SeeAllData=true in test class
- Only one setup method can be used in a test class.
- If there is any error in setup method. Entire class will be failed
Write effective Unit tests
To write an effective test cases, a developer needs to ensure to include the below points into the test class
- Use Test.startTest & Test.stopTest into each test method
- Use @isTest annotation for each method instead of using testMethod
- Develop cases for all scenarios if there are multiple if conditions
- Use System.assert, System.assertEquals to validate the result.
- Always test your Class/Trigger for at least 200 records
- Create the required test data in test class.
- Always use ( seeAllData = false ) at class or method level
Important thing to learn
TestDataFactory class Unit tests
TestDataFactory is an Apex Class which is used to generate the Test Data in a Class and the data can be used across multiple test classes.
This class is useful when multiple classes are using same data and avoid duplicate code, increase reusability & increase performance of the test case
Writing Test Classes in Salesforce
Write effective Unit tests/ test Classes
Episode 6 will be presented by Amit Singh on Feb 17, 2020 at 6 PM Indian Standard Time.
Complete below assignment to win $1000 Salesforce Voucher. Click here for rule.
| 1.Create Test class for the Assignment 5. Make sure that you have applied the following points|
•To generate the data use Test.LoadData method in TestDataFactory class
•TestDataFactory Class to prepare the data
•Use testSetup method in Test Class
•Must have both positive & negative testing
•Testing must be done for at-least 50 records
•Code coverage must be 95%
Please note that we have limit of 500 attendees that can join the online sessions. However, recording will be posted on our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.
So, learn at your pace and free will and ace your journey to Salesforce!
15,792 total views, 39 views today