1

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.

Matt Lacey
  • 25,618
  • 7
  • 66
  • 149
sumit
  • 479
  • 1
  • 8
  • 25
  • Bold doesn't work with the code formatting. I assume it's these lines?
    `a.G1_Count__c = a.Contacts__r.size();
    toUpdate.add(a);`
    
    – Matt Lacey Mar 17 '14 at 11:48

3 Answers3

1

I assume it's the two lines inside the conditional that aren't being covered, and it's because this code should be blowing up when it runs: you're not querying the G1_Count__cfield:

// Wrong
List<Account> aList = [Select (Select Id From Contacts__r) From Account a];

// Right
List<Account> aList = [Select Id, G1_Count__c, (Select Id From Contacts__r) From Account a];

You'll notice I also added the ID as you need that to do an update.

Matt Lacey
  • 25,618
  • 7
  • 66
  • 149
0

In such cases, use System.debug() to investigate suspected areas. Try below code, I also added some conditions to check the list for empty before updating which is good to have before a DML on a list.

global class UpdateCount implements Schedulable {

public static String CRON_EXP = '0 5 * * * ?';

  global void execute(SchedulableContext ctx) {

    List<Account> aList = [Select Id, G1_Count__c, (Select Id From Contacts__r) From Account a];
    List<Account> toUpdate = new List<Account>();
    for(Account a : aList) {
        System.debug('inside the for loop');
        System.debug('Contacts : '+a.Contacts__r.size());
        if(a.Contacts__r.size() > 0 && a.G1_Count__c != a.Contacts__r.size()) {
            System.debug('inside the if condition');
            a.G1_Count__c = a.Contacts__r.size();
            toUpdate.add(a);
        }
    }
    if(!toUpdate.isEmpty())
        Update toUpdate;
  }
}
highfive
  • 6,241
  • 3
  • 30
  • 56
  • 1
    highfive - you might be interested in this: http://salesforce.stackexchange.com/questions/19399/any-reason-to-skip-dml-on-empty-lists re your remark: check the list for empty before updating which is good to have before a DML – cropredy Mar 17 '14 at 19:58
  • It's great @crop1645. Didn't aware that SF take care of that. Thanks for the info. – highfive Mar 18 '14 at 03:36
0

It is because you are not creating any Contacts for your test data and so it does not fall into your if statement:

if(a.Contacts__r.size() > 0 && a.G1_Count__c != a.Contacts__r.size())
dphil
  • 6,185
  • 2
  • 36
  • 71