You need to sign in to do that
Don't have an account?
SFAdmin5
need help with 100% test coverage
I get 80% test coverage but want 100. Any ideas how I can get this class to pass 100%. Class below, followed by the 2 test methods. The failure is on the execute method in the class. Tests won't cover it and I can't figure out why
CLASS
global class deleteAccounts implements Database.Batchable<sObject>, Schedulable { String query = 'SELECT id from Account WHERE Name=test'; global deleteAccounts () { } global void execute(SchedulableContext SC) { deleteAccounts x = new deleteAccounts(); database.executebatch(x); } global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator([SELECT id FROM Account WHERE Name ='test']); } global void execute(Database.BatchableContext BC, List<sObject> scope){ delete scope; } global void finish(Database.BatchableContext BC){ }}
TEST METHODS:
public class TEST_deleteAccounts{ public static testMethod void testBatch() { List <Account> accns = new List<Account>(); for(integer i = 0; i<200; i++){ Account a = new Account(Name='test'+'i'); accns.add(a); } insert accns; Test.StartTest(); deleteAccounts x = new deleteAccounts(); ID batchprocessid = Database.executeBatch(x); Test.StopTest(); System.AssertEquals( database.countquery('SELECT COUNT()' +' FROM Account WHERE Name=test'), 0); } }
public class TEST_deleteAccountsschedjob{ public static testMethod void testBatch() { deleteAccounts a = new deleteAccounts(); String sch = '0 0 23 * * ?'; Test.startTest(); Id jobId = System.schedule('a', sch, a); // Get the CronTrigger info prior to the run CronTrigger cronTrigger1 = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :jobId]; // Assert that the expressions are the same System.assertEquals(sch, cronTrigger1.CronExpression); // Assert that the cron job has not started System.assertEquals(0, cronTrigger1.TimesTriggered); Test.stopTest(); // will execute the asynchronous Apex // Get the CronTrigger info prior to the run CronTrigger cronTrigger_after = [SELECT Id, CronExpression, TimesTriggered, NextFireTime FROM CronTrigger WHERE Id = :jobId]; // Assert that the cron job has run once System.assertEquals(1, cronTrigger_after.TimesTriggered); } }
I'm guessing, but I think your QueryLocator isn't running because the accounts that you're inserting is < 200.
Have you tried making it > 200 (maybe just 210 or so)? That should make the QueryLocator get called.
Thanks. I tried changing that and no luck. Nothing's covering that execute method
Hmm- dunno if its related but I found this thread:
http://boards.developerforce.com/t5/Apex-Code-Development/test-methods-for-Batch-Apex-is-not-causing-execute-method-to-run/td-p/154216
...And here:
http://boards.developerforce.com/t5/Apex-Code-Development/Batch-Apex-Testing-Issues/td-p/198335