Hi All,
Recently I came across this issue , In Testclass we used to create testrecords to test the functionalities of the code we have written ,but the problem here is like
whenever we insert the test records in test class we can't specify the CreatedDate since that is the ReadOnly Field.
In my scenario i will be creating task for the same business day for the lead when the user tries to create that in the business hours (9 AM to 5 PM -May varies
according to org) or else the task will be created in the next business day .
So the code tries to access the createdDate (time) and act according , So whenever we create the test record in the test class , the whole code will not be covered.
Code coverage here is based on the time we run the test class.
Inorder to get 100% code coverage we need to insert test records
#1.within Business hours
#2.Out of Business hours.
Actually inserting test records within /out of business hours is not possible until we are able to make createdDate field is dynamic .Solution is to make the createdDate field dyanmic for test class.
We can prepare the test records in a csv file - with createdDate(specified) and upload that in the static resource and we can refer that static resource file in the test class.
Static Resource File - TestLeadCreatedDate.csv
LastName,Company,CreatedDate
LastName1,Company1,2013-11-08T23:00:01.235-07:00
LastName2,Company2,2013-11-07T10:00:01.235-07:00
LastName3,Company3,2013-11-06T08:00:01.235-07:00
LastName4,Company4,2013-11-05T18:00:01.235-07:00
Recently I came across this issue , In Testclass we used to create testrecords to test the functionalities of the code we have written ,but the problem here is like
whenever we insert the test records in test class we can't specify the CreatedDate since that is the ReadOnly Field.
In my scenario i will be creating task for the same business day for the lead when the user tries to create that in the business hours (9 AM to 5 PM -May varies
according to org) or else the task will be created in the next business day .
So the code tries to access the createdDate (time) and act according , So whenever we create the test record in the test class , the whole code will not be covered.
Code coverage here is based on the time we run the test class.
Inorder to get 100% code coverage we need to insert test records
#1.within Business hours
#2.Out of Business hours.
Actually inserting test records within /out of business hours is not possible until we are able to make createdDate field is dynamic .Solution is to make the createdDate field dyanmic for test class.
We can prepare the test records in a csv file - with createdDate(specified) and upload that in the static resource and we can refer that static resource file in the test class.
- @istest
- public class TestLeadTaskAutoCreation{
- public static testMethod void test()
- {
- // TestLeadCreatedDate is the name of static resource which contains a CSV file with Test Lead Records that will be test inserted during test run.
- // We can make use of Test.loadData method instead of inserting test record in the test class - that can reduce the no of lines of code for the test class.
- //Created Date Field will not be Read only field when we use Test.loadData method .
- final String staticResourceName = 'TestLeadCreatedDate';
- final Lead[] testAccounts = (Lead[]) Test.loadData(Lead.sObjectType, staticResourceName);
- }
- }
Static Resource File - TestLeadCreatedDate.csv
LastName,Company,CreatedDate
LastName1,Company1,2013-11-08T23:00:01.235-07:00
LastName2,Company2,2013-11-07T10:00:01.235-07:00
LastName3,Company3,2013-11-06T08:00:01.235-07:00
LastName4,Company4,2013-11-05T18:00:01.235-07:00