Adding a Test Class

Prerequisites:

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.

Note
Testing is an important part of the development process. Before you can deploy Apex or package it for the Force.com AppExchange, the following must be true.
  • At least 75% of your Apex code must be covered by unit tests, and all of those tests must complete successfully.
    Note the following.
    • When deploying to a production organization, every unit test in your organization namespace is executed.
    • Calls to System.debug are not counted as part of Apex code coverage.
    • Test methods and test classes are not counted as part of Apex code coverage.
    • While only 75% of your Apex code must be covered by tests, your focus shouldn't be on the percentage of code that is covered. Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This should lead to 75% or more of your code being covered by unit tests.
  • Every trigger must have some test coverage.
  • All classes and triggers must compile successfully.
  1. From Setup, click Develop | Apex Classes and click New.
  2. In the class editor, add this test class definition, and then click Save.
    @isTest 
    private class HelloWorldTestClass {
        static testMethod 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 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.

    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;

    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:

    // 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 line is the actual test, verifying that the method applyDiscount actually ran and produced the expected result:

    // Test that the trigger correctly updated the price
    System.assertEquals(90, b.Price__c);
    
  3. Now let’s switch to the Developer Console to run this test and view code coverage information. Click Your Name | Developer Console. The Developer Console window opens.
  4. In the Developer Console, click Test | New Run.
  5. To add your test class, click HelloWorldTestClass, and then click >.
  6. 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.
  7. 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.
  8. 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.
By now, you have 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’ve sufficiently tested your code and you’re satisfied with it, you want to deploy the code along with any other prerequisite components to a production organization. The next step will show you how to do this for the code and custom object you’ve just created.
© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.