Testing Your Apex Code

Before you can upload a managed package, you must write and execute tests for your Apex code to meet minimum code coverage requirements. Also, all tests must run without errors when you upload your package to AppExchange.

To package your application and components that depend on Apex code, the following must be true.

This sample shows an Apex test class for a custom object that’s wired up to a component.
@isTest
class TestExpenseController {
    static testMethod void test() {
        //Create new expense and insert it into the database        
        Expense__c exp = new Expense__c(name='My New Expense',
                             amount__c=20, client__c='ABC',
                             reimbursed__c=false, date__c=null);
         ExpenseController.saveExpense(exp);

        //Assert the name field and saved expense
        System.assertEquals('My New Expense',
                           ExpenseController.getExpenses()[0].Name,
                          'Name does not match');
        System.assertEquals(exp, ExpenseController.saveExpense(exp));
    }
}
Note

Note

Apex classes must be manually added to your package.

For more information on distributing Apex code, see the Apex Code Developer's Guide.