Newer Version Available
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.
- At least 75% of your Apex code must be covered by unit tests, and all of those tests must
complete 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 are not counted as part of Apex code coverage.
- Test methods and test classes are not counted as part of Apex code coverage.
- While only 75% of your Apex code must be covered by tests, your focus shouldn't be on the percentage of code that is covered. Instead, you should make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This should lead to 75% or more of your code being covered by unit tests.
- Every trigger must have some test coverage.
- All classes and triggers must compile successfully.
This sample shows an Apex test
class that is used with the controller class in the expense tracker app available at Create a Standalone Lightning App.
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18class TestExpenseController {
19 static testMethod void test() {
20 //Create new expense and insert it into the database
21 Expense__c exp = new Expense__c(name='My New Expense',
22 amount__c=20, client__c='ABC',
23 reimbursed__c=false, date__c=null);
24 ExpenseController.saveExpense(exp);
25
26 //Assert the name field and saved expense
27 System.assertEquals('My New Expense',
28 ExpenseController.getExpenses()[0].Name,
29 'Name does not match');
30 System.assertEquals(exp, ExpenseController.saveExpense(exp));
31 }
32}