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.
- Choose the field which is used for where condition in the test class.
- Enter the values for the fields going to meet the criteria to increase the code coverage.
- Click Ok button.
- Click the Generate Test Class button.
- See your Test Class
In this step, Give the Property Value Name -> Click Save button.
The required Test Class is automatically generated.
- Save the Created Test Class “TestClassName_AC_Test”
Save the generated test class in the developer console: In Quick Search box, type Apex Class -> New -> Paste the generated Test Class and Save it as
- Run the Test Class and Check the Code Coverage
Finally, run the Test Class and check it in Handler. The code coverage is at 100 %.
- This is the simple and the easiest way to generate the test class for the Apex Trigger, Apex Class and Batch Class.
- This app helps to save the developer’s time and reduce workload.
- This app helps to easily increase the code coverage. Even we get the 100% code coverage.
In the Test Generator app, we can automatically generate the Test class code for our Apex Trigger, Controller and Batch Class. This generated Test Class will cover the 100% of the code. So, it helps the developer to save their development time and reduces the workload pressures. This app helps to include the Positive and negative causes for a single and bulk record processing.
Use this app when we need to do the small changes or add a small logic in our code. This app is the best way to automatically generate the Test Class code. The future enhancements of this app is the test classes for Callouts, VF Pages codes.