Developing Secure Code
Testing Your Apex Code
Making API Calls from Apex
Creating Components in Apex
Testing Components
Previous PDF Versions
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.
Unit tests must cover at least 75% of your Apex code, and all of those tests must complete successfully.
Note the following.
System.debug aren’t counted as part of Apex code coverage.Every trigger must have some test coverage.
All classes and triggers must compile successfully.
This sample shows an Apex test class for a custom object that’s wired up to a component.
1@isTest
2class TestExpenseController {
3 static testMethod void test() {
4 //Create new expense and insert it into the database
5 Expense__c exp = new Expense__c(name='My New Expense',
6 amount__c=20, client__c='ABC',
7 reimbursed__c=false, date__c=null);
8 ExpenseController.saveExpense(exp);
9
10 //Assert the name field and saved expense
11 System.assertEquals('My New Expense',
12 ExpenseController.getExpenses()[0].Name,
13 'Name does not match');
14 System.assertEquals(exp, ExpenseController.saveExpense(exp));
15 }
16}Apex classes must be manually added to your package.
Note
For more information on distributing Apex code, see Debugging, Testing, and Deploying Apex in the Apex Developer Guide.
See Also