Unit Testing Apex

To facilitate the development of robust, error-free code, Apex supports the creation and execution of unit tests. Unit tests are class methods that verify whether a particular piece of code is working properly. Unit test methods take no arguments, commit no data to the database, send no emails, and are flagged with the testMethod keyword in the method definition.

For example:
public class myClass {
    static testMethod void myTest() {
        code_block
    }
}
Note
Test methods cannot be used to test Web service callouts. Web service callouts are asynchronous, while unit tests are synchronous.
Use the isTest annotation to define classes or individual methods that only contain code used for testing your application. The isTest annotation is similar to creating methods declared as testMethod.
Note
Classes defined with the isTest annotation don't count against your organization limit of 2 MB for all Apex code. Individual methods defined with the isTest annotation do count against your organization limits. See Understanding Execution Governors and Limits.

This is an example of a test class that contains two test methods.

@isTest
private class MyTestClass {

   // Methods for testing 
    
   @isTest static void test1() {
      // Implement test code 
    
   }

   @isTest static void test2() {
      // Implement test code 
    
   }

}
© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.