I have used schedule in class for my code. I am not able to cover code 100%. Its just cover 64% even i have write all condition.
Class
public void execute(SchedulableContext sc){
Integer Year = Date.Today().Year();
Boolean myData = false;
List<Employment_Data__c> listEmp = new List<Employment_Data__c>();
List<Employment_Data__c> edata = [Select id, name from Employment_Data__c];
for(Employment_Data__c empData : edata){
if(empdata.name == String.valueOf(Year)){
myData = true;
break;
}
}
List<Account> accData = [Select id, Name, Service_ID__c from Account where Service_ID__c != null];
System.debug('Total ::'+accData.size());
if(myData != true){ // This condition for fulfilled in my test class
for(Account act : accData){
Employment_Data__c ed = new Employment_Data__c();
ed.name = String.valueOf(Year);
ed.County__c = act.Id;
listEmp.add(ed);
}
insert listEmp;
}
}
Test class
static testMethod void testCreateEmpData(){
Test.startTest();
Database.QueryLocator QL;
Database.BatchableContext BC;
SchedulableContext sc;
ScheduleCreateRecord SE = new ScheduleCreateRecord();
Account act = new Account(Name='testAct');
insert act;
Integer Year = Date.Today().Year();
Year = Year + 1;
Employment_Data__c ed = new Employment_Data__c(Name = String.valueOf(Year),County__c = act.Id);
insert ed;
SE.execute(sc);
Test.stopTest();
}
In my Employment_Data__c object, name is all year like 2001, 2002 etc. I want to insert one record of next year using this schedule class. i.e. if i run this class now it will create one record for 2015. Give me suggestion how to cover that all code.
Thanks, vml
Test.startTest()
before you've even created your data necessary for it to run. You'll also want to assert results after the class runs; something that should happen at the end of your method, not at the beginning..