How to Add Test Classes in Salesforce

You need to use the right test method in order to successfully add test classes in Salesforce. The test methods you wish to use will be important if you intend to package your code on AppExchange or Force.com. When adding a test class, you will use just one test method and it is also important to run the test and identify the code coverage.

To find the code coverage, you will take the number of exclusive Apex code lines, which are the ones that have been executed when you were carrying out the execution of the test method. You will then divide the codes by the total number of Apex code lines present in the classes and trigger. However, you should keep in mind that the number of lines will not include the code lines amid your test Methods. 

The method of test you choose to use will validate and exercise the class and triggers, as well as enabling you to generate a code coverage of 100% for the class and trigger. Testing is vital in the process of development and prior to deploying the package or Apex, you will need to ensure that:

  • More than 75% of your Apex code is covered by the unit test, as well as ensuring that the test included are complete
  •  Each trigger should have several test coverage in order to be successful
  •  Each class and trigger should successfully compile


In addition, you should ensure that each unit test that is in the namespace of your organization is executed. Also, make sure that the Calls to System.debug and test methods have not been included as a section of the coverage of Apex code. Also confirm that each case use of the application is included in the 75% coverage. 

Click Salesforce trigger best practices to read more about triggers.

Test Classes in Salesforce

How to Add Test Classes in Salesforce

1. You will start from the Setup, then click on Develop, Apex Classes then Click on New. 

2. Add the definition of the test class in the Class Editor, then click on Save. 

The class created will be defined by the footnote @isTest. Any class that is defined with such annotation can only have test methods in them. It is beneficial to create a detached class for the sake of testing because a class that has been defined by @isTest will not not exceed the 3MB limit for every Apex code.

You may also include the @isTest annotation to each method. On the other hand, the ValidateHelloWorld is described as a test Method. That implies that if there is any alteration made in the database, then they will be rolled back automatically when the execution is over. You will not be required to delete any test data that has been created in the method validateHelloWorld. 

Initially, the test method will create a new book and then the book will be inserted into the database, but only for a short period of time. Then, the cost value will be written in the debug log by the System.debug. 

Having the book inserted in the database, the code will then retrieve the book, which has just been inserted, via the ID that was assigned to it in the first place when it was being inserted. The code will then log the new price that has been modified by the trigger. The Price_c field will be updated as the MyHelloWorld class is running. In turn, the value of the field will be reduced by 10%. 

Once you have created the test class and run it, you will now need to move on to the Developer Console. This will now enable you to run the test that you have created and view the information about the code coverage. You will click on Your Name, then Developer Console. This will open the Developer Console Window. 

3. Click on Test, then New Run inside the Developer Console Window. 

4. Click on HelloWorldTestClass in order to add the test classes to Salesforce, then click the > key. 

5. Finally, Click on Run to run your test. 

Here, the results of the test will be displayed in the Tests tab. Alternatively, you may enlarge the test class inside the Tests tab in order to see the methods that were executed. Here, the class will only have a single test method. 

6. When that is done, the Overall Code Coverage window will display the code coverage of your created test class. In order to view the code lines that are on the trigger and covered by the test that you have just created, you will double click on the HelloWorldTrigger code coverage lines.

The trigger typically calls a method from MyHelloWorld class, therefore, that implies that class will also have 100% coverage double click on MyHelloWorld in order to view the class coverage. 

7. Double click on the log lines that are the most recent in the logs list in order to open the log file. Once it opens, it will display the execution log, which includes logging info about the trigger event. The debug output of the price prior and subsequent to the trigger, as well as the applyDiscount class method call will also be included. 

When you finally open the log file, you will have terminated each step that is essential for writing a number of Apex code using a test that operates inside your environment of development. Technically, you would want to deploy the code together with any other required elements to a production organization. When you are through with that, you will then need to indicate how you can accomplish this for the sake of the custom object and code that you have newly created. Go to salesforce trigger tutorial for additional trigger information.

Conclusion


Normally, the test method that you use are not aimed at blocking your development, instead, they help in making the creating successful. The test methods that you use are supposed to be written in the event of development effort. Having the right test method will help to give you a mechanical test collection that helps you to have a quality test class. You will be required to run the test and confirm the code coverage when adding test classes in Salesforce.

People who read this article were also interested this read on writing test class in Salesforce. 

Amanda McDonnald
Amanda is the Lead Author & Editor of Rainforce Blog. Amanda established the Rainforce blog to create a source for news and discussion about some of the issues, challenges, news, and ideas relating to Salesforce usage.
Amanda McDonnald on sabtwitterAmanda McDonnald on sabgoogle