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:

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.debug aren’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.

Note

  1. From Setup, enter Apex Classes in the Quick Find box, then select Apex Classes and click New.
  2. 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.

    The @IsTest annotation on methods is equivalent to the testMethod keyword. As best practice, Salesforce recommends that you use @IsTest rather than testMethod. The testMethod keyword may be versioned out in a future release.

    Note

    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);
  3. To run this test and view code coverage information, switch to the Developer Console.
  4. In the Developer Console, click Test | New Run.
  5. To select your test class, click HelloWorldTestClass.
  6. To add all methods in the HelloWorldTestClass class to the test run, click Add Selected.
  7. 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.
  8. 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.
  9. 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.
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.