Add a Test Class
- A Salesforce account in a sandbox Professional, Enterprise, Performance, or Unlimited Edition org, or an account in a Developer org.
- The HelloWorldTrigger Apex trigger.
- From Setup, enter Apex Classes in the Quick Find box, then select Apex Classes and click New.
-
In the class editor, add this test class definition, and then click
Save.
@IsTest private class HelloWorldTestClass { @IsTest static void validateHelloWorld() { Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100); System.debug('Price before inserting new book: ' + b.Price__c); // Insert book insert b; // Retrieve the new book b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id]; System.debug('Price after trigger fired: ' + b.Price__c); // Test that the trigger correctly updated the price System.assertEquals(90, b.Price__c); } }
This class is defined using the @IsTest annotation. 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 @IsTest don’t count against your org’s limit of 6 MB of Apex code. You can also add the @IsTest annotation to individual methods. For more information, see @IsTest Annotation and Execution Governors and Limits.
The method validateHelloWorld is defined using the @IsTest annotation. 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.
First, the test method creates a book and inserts it into the database temporarily. The System.debug statement writes the value of the price in the debug log.
Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100); System.debug('Price before inserting new book: ' + b.Price__c); // Insert book insert b;
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.debug statement then logs the new price that the trigger modified.
// Retrieve the new book b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id]; System.debug('Price after trigger fired: ' + b.Price__c);
When the MyHelloWorld class runs, it updates the Price__c field and reduces its value by 10%. The following test verifies that the method applyDiscount ran and produced the expected result.
// Test that the trigger correctly updated the price System.assertEquals(90, b.Price__c);
- To run this test and view code coverage information, switch to the Developer Console.
- In the Developer Console, click .
- To select your test class, click HelloWorldTestClass.
- To add all methods in the HelloWorldTestClass class 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 MyHelloWorld class, 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 applyDiscount method, and the price before and after the trigger.