No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
Newer Version Available
Adding a Test Class
- A Salesforce account in a sandbox Performance, Unlimited, or Enterprise Edition organization, or an account in a Developer organization.
- The HelloWorldTrigger Apex trigger.
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.
- From Setup, click and click New.
- In the class editor, add this test class definition, and
then click Save.
1swfobject.registerObject("clippy.codeblock-0", "9");@isTest 2private class HelloWorldTestClass { 3 static testMethod void validateHelloWorld() { 4 Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100); 5 System.debug('Price before inserting new book: ' + b.Price__c); 6 7 // Insert book 8 insert b; 9 10 // Retrieve the new book 11 b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id]; 12 System.debug('Price after trigger fired: ' + b.Price__c); 13 14 // Test that the trigger correctly updated the price 15 System.assertEquals(90, b.Price__c); 16 } 17} 18This class is defined using the @isTest annotation. Classes defined as such can only contain test methods. One advantage to creating a separate class for testing is that classes defined with isTest don't count against your organization limit of 3 MB for all Apex code. You can also add the @isTest annotation to individual methods. For more information, see IsTest Annotation and Understanding Execution Governors and Limits.
The method validateHelloWorld is defined as a testMethod. This means that if any changes are made to the database, they are automatically rolled back when execution completes and you don't have to delete any test data created in the test method.
First the test method creates a new book and inserts it into the database temporarily. The System.debug statement writes the value of the price in the debug log.
1Book__c b = new Book__c(Name='Behind the Cloud', Price__c=100); 2System.debug('Price before inserting new book: ' + b.Price__c); 3 4// Insert book 5insert b;Once 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, and then logs the new price that the trigger modified:
1// Retrieve the new book 2b = [SELECT Price__c FROM Book__c WHERE Id =:b.Id]; 3System.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 line is the actual test, verifying that the method applyDiscount actually ran and produced the expected result:
1// Test that the trigger correctly updated the price 2System.assertEquals(90, b.Price__c); - Now let’s switch to the Developer Console to run this test and view code coverage information. Click . The Developer Console window opens.
- In the Developer Console, click .
- To add your test class, click HelloWorldTestClass, and then click >.
- To run the test, 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 lines of code in the trigger covered by this test, which is 100%, double-click the code coverage line for HelloWorldTrigger. Also, because the trigger calls a method from the MyHelloWorld class, this class has some coverage too (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 class method, and the debug output of the price before and after the trigger.