• AnthonyEC
  • NEWBIE
  • 25 Points
  • Member since 2011

  • Chatter
    Feed
  • 1
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 4
    Replies

Hi,

having a little trouble getting test coverage on a batch class, odly the test passes like everything was executed, but the execute method isn't covered. Been scratching my head on this one for a while.

 

The class itself is fairly simple, and according to the test, its doing what its suposed to be. Running it in executeanon works as expected as well.

 

Class:

 

 line source
 1  global class updateContactBatch implements Database.Batchable<sObject>
 2  {
 3  
 4   String Query;
 5   List<Contact> uC = new List<Contact>();
 6   List<Master_Tracker__c> uM = new List<Master_Tracker__c>();
 7  
 8   global Database.QueryLocator start(Database.BatchableContext BC)
 9   {
 10   return Database.getQueryLocator(query);
 11   }
 12  
 13   global void execute(Database.BatchableContext BC, List<sObject> scope)
 14   {
 15   for(sObject s : scope)
 16   {
 17   Master_Tracker__c m = (Master_Tracker__c)s;
 18   Contact c = new Contact(Id = m.Contact__c, Send_ExactTarget_Email__c = m.Contact_Exact_Target_Send__c);
 19   m.ContactUpdate__c = false;
 20   uM.add(m);
 21   uC.add(c);
 22   }
 23   update uC;
 24   update uM;
 25   }
 26  
 27   global void finish(Database.BatchableContext BC)
 28   {
 29   System.debug('Finish');
 30   }
 31  
 32   public static testMethod void testBatch()
 33   {
 34  
 35  
 36   List<Contact> cTest = [Select Id from Contact limit 10];
 37   List<Account> aTest = [Select Id from Account limit 1];
 38   List<Master_Tracker__c> mU = new List<Master_Tracker__c>();
 39   for(integer i = 0; i<10; i++)
 40   {
 41   Master_Tracker__c m = new Master_Tracker__c(School_Studies__c = 'testing', Contact__c = cTest[i].Id, BID__c = 'test' + i, Account_Name__c = aTest[0].Id, Offer_Id__c = 'test2' + i, Initiated_Date__c = Date.today());
 42   mU.add(m);
 43   }
 44   insert mU;
 45   Test.StartTest();
 46   updateContactBatch upCon = new updateContactBatch();
 47   List<Master_Tracker__c> testing = [SELECT m.Id, m.Contact__c, m.Contact_Exact_Target_Send__c FROM Master_Tracker__c m WHERE m.ContactUpdate__c = true];
 48   System.debug('the number of returned records are ' + testing.size());
 49   upCon.query = 'SELECT m.Id, m.Contact__c, m.Contact_Exact_Target_Send__c FROM Master_Tracker__c m WHERE m.ContactUpdate__c = true';
 50   Id batchProcessId = Database.executeBatch(upCon);
 51  
 52   Test.StopTest();
 53  
 54   AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id = :batchProcessId];
 55   System.debug('\n\nFinal results are: '+a);
 56   System.AssertEquals('Completed', a.status);
 57   System.AssertEquals(0, a.NumberOfErrors);
 58  
 59   List<Master_Tracker__c> m2 = [SELECT Id FROM Master_Tracker__c WHERE School_Studies__c = 'testing' and ContactUpdate__c = false];
 60   System.AssertEquals(m2.Size(), 10);
 61   }
 62  
 63  }

 

Here are the results of the Async job:

 

Final results are: AsyncApexJob:{Status=Completed, NumberOfErrors=0, CreatedById=005A0000000S87QIAS, Id=707T0000000EXg3IAG, TotalJobItems=1, JobItemsProcessed=1}

 

The job was submitted and completed, which should mean the code is covered, or at the least, part of it. Any help or places to look would be much apreciated.

 

Hi,

having a little trouble getting test coverage on a batch class, odly the test passes like everything was executed, but the execute method isn't covered. Been scratching my head on this one for a while.

 

The class itself is fairly simple, and according to the test, its doing what its suposed to be. Running it in executeanon works as expected as well.

 

Class:

 

 line source
 1  global class updateContactBatch implements Database.Batchable<sObject>
 2  {
 3  
 4   String Query;
 5   List<Contact> uC = new List<Contact>();
 6   List<Master_Tracker__c> uM = new List<Master_Tracker__c>();
 7  
 8   global Database.QueryLocator start(Database.BatchableContext BC)
 9   {
 10   return Database.getQueryLocator(query);
 11   }
 12  
 13   global void execute(Database.BatchableContext BC, List<sObject> scope)
 14   {
 15   for(sObject s : scope)
 16   {
 17   Master_Tracker__c m = (Master_Tracker__c)s;
 18   Contact c = new Contact(Id = m.Contact__c, Send_ExactTarget_Email__c = m.Contact_Exact_Target_Send__c);
 19   m.ContactUpdate__c = false;
 20   uM.add(m);
 21   uC.add(c);
 22   }
 23   update uC;
 24   update uM;
 25   }
 26  
 27   global void finish(Database.BatchableContext BC)
 28   {
 29   System.debug('Finish');
 30   }
 31  
 32   public static testMethod void testBatch()
 33   {
 34  
 35  
 36   List<Contact> cTest = [Select Id from Contact limit 10];
 37   List<Account> aTest = [Select Id from Account limit 1];
 38   List<Master_Tracker__c> mU = new List<Master_Tracker__c>();
 39   for(integer i = 0; i<10; i++)
 40   {
 41   Master_Tracker__c m = new Master_Tracker__c(School_Studies__c = 'testing', Contact__c = cTest[i].Id, BID__c = 'test' + i, Account_Name__c = aTest[0].Id, Offer_Id__c = 'test2' + i, Initiated_Date__c = Date.today());
 42   mU.add(m);
 43   }
 44   insert mU;
 45   Test.StartTest();
 46   updateContactBatch upCon = new updateContactBatch();
 47   List<Master_Tracker__c> testing = [SELECT m.Id, m.Contact__c, m.Contact_Exact_Target_Send__c FROM Master_Tracker__c m WHERE m.ContactUpdate__c = true];
 48   System.debug('the number of returned records are ' + testing.size());
 49   upCon.query = 'SELECT m.Id, m.Contact__c, m.Contact_Exact_Target_Send__c FROM Master_Tracker__c m WHERE m.ContactUpdate__c = true';
 50   Id batchProcessId = Database.executeBatch(upCon);
 51  
 52   Test.StopTest();
 53  
 54   AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id = :batchProcessId];
 55   System.debug('\n\nFinal results are: '+a);
 56   System.AssertEquals('Completed', a.status);
 57   System.AssertEquals(0, a.NumberOfErrors);
 58  
 59   List<Master_Tracker__c> m2 = [SELECT Id FROM Master_Tracker__c WHERE School_Studies__c = 'testing' and ContactUpdate__c = false];
 60   System.AssertEquals(m2.Size(), 10);
 61   }
 62  
 63  }

 

Here are the results of the Async job:

 

Final results are: AsyncApexJob:{Status=Completed, NumberOfErrors=0, CreatedById=005A0000000S87QIAS, Id=707T0000000EXg3IAG, TotalJobItems=1, JobItemsProcessed=1}

 

The job was submitted and completed, which should mean the code is covered, or at the least, part of it. Any help or places to look would be much apreciated.