Have you
ever been come up with a case where you have to create test data with created
date less than today. Is that possible??
In winter’13
salesforce introduced a static method on test class, loadData() method which
can load the data from static resource. This method takes two parameters
1.Sobject
token – to identify to which object the static data should be mapped.
2.Static
resource name- From which salesforce load data, it can be a csv file with field
names are file headers and respective cells having values.
Lets say I
have a following piece of code which I need to cover from a test class.
public class TestLoad
{
public void TestAccounts()
{
List<Account> a = [select name from account where createdDate <:system.today()];
if(a.size()>0)
{
for(Account ac: a)
{
//Process some logic
}
}
}
}
{
public void TestAccounts()
{
List<Account> a = [select name from account where createdDate <:system.today()];
if(a.size()>0)
{
for(Account ac: a)
{
//Process some logic
}
}
}
}
I can use
the following code to cover the complete line of code.
@isTest
private class TestLoad_TC
{
static testMethod Void TestLoad_TC()
{
Test.startTest();
List<sObject> ls = Test.loadData(Account.sObjectType,'testAccounts');
TestLoad insta1 = new TestLoad();
insta1.TestLoad2();
Test.stopTest();
}
}
private class TestLoad_TC
{
static testMethod Void TestLoad_TC()
{
Test.startTest();
List<sObject> ls = Test.loadData(Account.sObjectType,'testAccounts');
TestLoad insta1 = new TestLoad();
insta1.TestLoad2();
Test.stopTest();
}
}
Here
Test.loadData(Account.sObjectType,'testAccounts'), which loads the data from
static resource.
The csv file
headers should maintain the object field api name for the method to map the
data from file to object.
No comments:
Post a Comment