here I created scheduler class that will schedule at every 5 mins. and G1_count field will update in every 5 min. i have covered 77% of code but I want it up to 100%...
I don't know what I am missing with this, please see the scheduler class and test classes below.
Scheduler class:
global class UpdateCount implements Schedulable {
public static String CRON_EXP = '0 5 * * * ?';
global void execute(SchedulableContext ctx) {
List<Account> aList = [Select (Select Id From Contacts__r) From Account a];
List<Account> toUpdate = new List<Account>();
for(Account a : aList) {
if(a.Contacts__r.size() > 0 && a.G1_Count__c != a.Contacts__r.size()) {
a.G1_Count__c = a.Contacts__r.size();
toUpdate.add(a);
}
}
Update toUpdate;
}
}
Test class for above class:
@istest
class TestUpdateCount {
static testmethod void test() {
Test.StartTest();
Account a = new Account();
a.name ='abc';
a.G1_Count__c = 10;
insert a;
// Schedule the test job
String jobId = System.schedule('testBasicScheduledApex',
UpdateCount.CRON_EXP,
new UpdateCount());
// Get the information from the CronTrigger API object
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered,
NextFireTime
FROM CronTrigger WHERE id = :jobId];
system.debug('ct___'+ct );
// Verify the expressions are the same
System.assertEquals(UpdateCount.CRON_EXP,
ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
// Verify the next time the job will run
// System.assertEquals('2014-03-17 05:05:00', String.valueOf(ct.NextFireTime));
System.assertNotEquals(11,[Select (Select Id From Contacts__r),G1_Count__c From Account a].G1_Count__c);
Test.stopTest();
System.assertEquals(10,[Select (Select Id From Contacts__r),G1_Count__c From Account a].G1_Count__c);
}
}
When I checked for code coverage two lines in the code are not covered, but I can't work out why.