Test classes in salesforce are used to make sure that any Apex customization you try to deploy into an org will operate properly. Salesforce will run all test classes and if they don’t all pass, you will not be able to deploy code to the org. This gives Developer a great deal of assurance that your code is behaving properly as long as you take the time to write good test classes. .
Before the developer can deploy our code or package to the Production, at least 75% of Apex code must be covered when run the Test Class, and all those tests must pass and each trigger must have maximum of code coverage. Even though code coverage is a requirement for deployment, don’t write code coverage only to meet this requirement. Make sure to test the common use cases in our Test Class, including positive and negative test cases, and for bulk and single record processing.
The developer manually develops the Test Class code and spends more time for that. They also face some issues and difficulty to increase the code coverage and develop the positive and negative test cases for bulk and single record processing.
To minimize the developer workload and save the development time, we can use “Test Class Generator” App form the AppExchange. This app is the managed package which is freely available in Salesforce AppExchange. Test Class Generator app is a simple, easy and intuitive app, and it helps to automatically generate Test Class for Apex Trigger, Controller and Batch. We need to follow 3 simple steps in order to automatically generate Test Class for our developed code. This app will help the developers to develop unit tests easily and improve efficiency.
The following steps is same for Apex Trigger, Controller and Batch Class.
- Install “Test Class Generator” in Salesforce from the AppExchange.
- Open the “Test Generator” app in salesforce and do the following three steps to automatically generate the Test Class.
- Choose Class: In this step, we can choose the Class Name and Variables, Properties and Parameters.
- Build: In this Step, we can choose the object, required fields and give the specific value in Where class which we need to use in Test Class.
- See your Test Class: In this step test class is generated as per our requirement.
- Copy the generated Test Class and Paste it in Apex Test Class and save it.
- Run the Test Class and will check the code coverage in the Controller and the Apex Trigger.
- Install “Test Class Generator” in Salesforce from the AppExchange
Steps: Go to AppExchange in Salesforce -> open the Test Generator App -> install this app in our Org -> In Salesforce, Type Installed Packages in quick search box -> click this app name and view this app component details.
- Open the “Test Generator” app in Salesforce and do the three steps to automatically generate the Test Class
Open Test Generator app in the Salesforce -> do the following three steps to automatically generate the Test Class.
- Choose Class
In this step, we can choose the controller name, Properties, Variables and Parameters for the Test Class.
- In the first step, Choose Class.
- Select the Controller name “AccountHandler_AC”.
- Click the Parse button.
- Choose the needed Properties, Variable and Parameter.
- Build Data
In this step, we can choose the object, fields that we want to use in the test class and use the where clause to meet the conditions in the developed Apex Class. In the where clause, we give the specific values to meet the criteria to cover the code in the Apex Class.
For Example: In the screenshots, I chose the AccountHandler_AC Class. This class is used to generate the PDF when the Account’s Industry Field is Engineering. So, in the Where Clause, Choose Industry field and give the Value as Engineering (Step 5,6 and Step 7).
- In the Build Data step.
- Choose the object for the test class.
- Click the Choose Object fields button to list the fields that are all available in the selected object
- Choose the fields which are used/needed for the test class.