Test Class Generator App

Test classes are used by Salesforce 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.

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.

Note: 

The following steps is same for Apex Trigger, Controller and Batch Class.

Steps 

  1. Install “Test Class Generator” in Salesforce from the AppExchange.
  2. Open the “Test Generator” app in salesforce and do the following three steps to automatically generate the Test Class.
  3. Choose Class: In this step, we can choose the Class Name and Variables, Properties and Parameters.
  4. 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.
  5. See your Test Class: In this step test class is generated as per our requirement.
  6. Copy the generated Test Class and Paste it in Apex Test Class and save it.
  7. Run the Test Class and will check the code coverage in the Controller and the Apex Trigger.
  8. 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.

test class salesforce
  1. 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.

test class in salesforce
  1. Choose Class

In this step, we can choose the controller name, Properties, Variables and Parameters for the Test Class.

test class in salesforce
  1. In the first step, Choose Class.
  2. Select the Controller name “AccountHandler_AC”.
  3. Click the Parse button.
  4. Choose the needed Properties, Variable and Parameter.
  1. 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).

test class in salesforce
  1. In the Build Data step.
  2. Choose the object for the test class.
  3. Click the Choose Object fields button to list the fields that are all available in the selected object
  4. Choose the fields which are used/needed for the test class.
test class in salesforce
  1. Choose the field which is used for where condition in the test class.
  2. Enter the values for the fields going to meet the criteria to increase the code coverage.
  3. Click Ok button.
  4. Click the Generate Test Class button.
  1. See your Test Class

In this step, Give the Property Value Name -> Click Save button.

how to create test class in salesforce  The required Test Class is automatically generated.

how to create test class in salesforce
  1. 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

how to create test class in salesforce
  1. 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 %.

how to create test class in salesforce

Pros 

  1. This is the simple and the easiest way to generate the test class for the Apex Trigger, Apex Class and Batch Class.
  2. This app helps to save the developer’s time and reduce workload.
  3. This app helps to easily increase the code coverage. Even we get the 100% code coverage.

Conclusions 

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.

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.