Add a Test Class
In this step, you add a test class with one test method. You also run the test and verify code coverage. The test method exercises and validates the code in the trigger and class. Also, it enables you to reach 100% code coverage for the trigger and class.
Prerequisites:
- A Salesforce account in a sandbox Professional, Enterprise, Performance, or Unlimited Edition org, or an account in a Developer org.
- The HelloWorldTrigger Apex trigger.
Testing is an important part of the development process. Before you can deploy Apex or package it for AppExchange, the following must be true.
-
Unit tests must cover at least 75% of your Apex code, and all of those tests must complete successfully.
-
Every trigger must have some test coverage.
-
All classes and triggers must compile successfully.
-
Note the following.
- When deploying Apex to a production organization, each unit test in your organization namespace is executed by default.
- Calls to
System.debugaren’t counted as part of Apex code coverage. - Test methods and test classes aren’t counted as part of Apex code coverage.
- While only 75% of your Apex code must be covered by tests, don’t focus on the percentage of code that is covered. Instead, make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This approach ensures that 75% or more of your code is covered by unit tests.
-
From Setup, enter
Apex Classesin theQuick Findbox, then select Apex Classes and click New. -
In the class editor, add this test class definition, and then click Save.
This class is defined using the
@IsTestannotation. Classes defined this way should only contain test methods and any methods required to support those test methods. One advantage to creating a separate class for testing is that classes defined with@IsTestdon’t count against your org’s limit of 6 MB of Apex code. You can also add the@IsTestannotation to individual methods. For more information, see@IsTestAnnotation and Execution Governors and Limits.The method
validateHelloWorldis defined using the@IsTestannotation. This annotation means that if changes are made to the database, they’re rolled back when execution completes. You don’t have to delete any test data created in the test method.The
@IsTestannotation on methods is equivalent to thetestMethodkeyword. As best practice, Salesforce recommends that you use@IsTestrather thantestMethod. ThetestMethodkeyword may be versioned out in a future release.First, the test method creates a book and inserts it into the database temporarily. The
System.debugstatement writes the value of the price in the debug log.After the book is inserted, the code retrieves the newly inserted book, using the ID that was initially assigned to the book when it was inserted. The
System.debugstatement then logs the new price that the trigger modified.When the
MyHelloWorldclass runs, it updates thePrice__cfield and reduces its value by 10%. The following test verifies that the methodapplyDiscountran and produced the expected result. -
To run this test and view code coverage information, switch to the Developer Console.
-
In the Developer Console, click Test | New Run.
-
To select your test class, click HelloWorldTestClass.
-
To add all methods in the
HelloWorldTestClassclass to the test run, click Add Selected. -
Click Run.
The test result displays in the Tests tab. Optionally, you can expand the test class in the Tests tab to view which methods were run. In this case, the class contains only one test method.
-
The Overall Code Coverage pane shows the code coverage of this test class. To view the percentage of lines of code in the trigger covered by this test, which is 100%, double-click the code coverage line for HelloWorldTrigger. Because the trigger calls a method from the
MyHelloWorldclass, this class also has coverage (100%). To view the class coverage, double-click MyHelloWorld. -
To open the log file, in the Logs tab, double-click the most recent log line in the list of logs. The execution log displays, including logging information about the trigger event, the call to the
applyDiscountmethod, and the price before and after the trigger.
By now, you’ve completed all the steps necessary for writing some Apex code with a test that runs in your development environment. In the real world, after you tested your code and are satisfied with it, you want to deploy the code and any prerequisite components to a production org. The next step shows you how to do this deployment for the code and custom object you created.
See Also