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.
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.
You can run unit tests for:
– A specific class
– A subset of classes
– All unit tests in your organization
To run a test, use any of the following:
– The Salesforce user interface
– The Force.com IDE
– The API
Required Average test coverage across all Apex Classes and Triggers is 75%.
Considerations:
- Test methods cannot be used to test Web service API.
- You can’t send email from test method
- Since test method doesn’t commit data creation, you need not delete upon completion.
- In Feed tracking, they don’t result in creation of Feed Tracked changes records.
Example:
Sample Class:
public class SampleClass
{
public void insertAcct(String name, String extId)
{
Account acc = new Account(Name = name,External_ID__c = extId);
insert acc;
}
}
Test Class for Sample Class:
@isTest
private class MyTestClass
{
static testMethod void insertAcc()
{
SampleClass sc = new SampleClass();
sc.insertAcct(‘Test Account’,’100′);
}
}