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.

Testing and unit tests are an important part of the development process.
  • You must have at least 75% of your Apex covered by unit tests to deploy your code to production environments. In addition, all triggers must have some test coverage.
  • We recommend that you have 100% of your code covered by unit tests, where possible.
  • Calls to System.debug are not counted as part of Apex code coverage in unit tests.
  1. Click Your Name | Setup | 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 as opposed to adding test methods to an existing class is that classes defined with isTest don't count against your organization limit of 2 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. Click Run Test in the class page to run all the test methods in this class. In this case, we have only one test method. The Apex Test Result page appears after the test finishes execution. It contains the test result details such as the number of test failures, code coverage information, and a link to a downloadable log file.
  4. Click Download and select to open the log file. You can find 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. Alternatively, you can use the Developer Console for debugging Apex code. See “Developer Console” in the Salesforce online help.
  5. You can also run the test through the Apex Test Execution page, which runs the test asynchronously, which means that you don't have to wait for the test run to finish to get the test result, but you can perform other tasks in the user interface while the test is still running and then visit this page later to check the test status.
    1. Click Your Name | Setup | Develop | Apex Test Execution.
    2. Click Run Tests.
    3. Select the class HelloWorldTestClass, and then click Run.
    After a test finishes running, you can:
    • Click the test to see result details. If a test fails, the first error message and the stack trace display.
    • Click View to see the source Apex code.
  6. After the test execution completes, verify the amount of code coverage.
    1. Click Your Name | Setup | Develop | Apex Classes.
    2. Click Calculate your organization's code coverage to see the amount of code in your organization that is covered by unit tests.
    3. In the Code Coverage column, click 100% to see the lines of code covered by unit tests.
    Take a look at the list of triggers by clicking Your Name | Setup | Develop | Apex Triggers. You'll see that the trigger you wrote also has 100% of its code covered.
By now, you completed all the steps necessary for having some Apex code that has been tested and 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–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.