0

I have created a Batch Apex class and its associated Scheduler class. The classes work fine. I am now writing the test class for it but for some reason it just does not seem to cover the scheduler class. The test is passing because it completes but it actually does not cover the scheudler class. The apex code I wrote I wrote for the batch job is 100% covered, however. Do I need to add something extra in the test class to cover the scheduler class? I thought with the batch job it would take care of the scheduler class? Can someone help please?

The Batch Apex:

global class StatusSync implements Database.Batchable<sObject>, Database.AllowsCallouts {
    public string queryStr;
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(queryStr);
    }

    global void execute(Database.BatchableContext BC, List<Ticket__c> records) { //this method is completely uncovered by test. Seems like it didnt enter it

        AnotherClassServiceHandler.doStatusUpdate(records);
    }   

    global void finish(Database.BatchableContext BC){ 
        
    }
}

This is the scheduler class I wrote:

global class StatusSyncSheduler implements Schedulable {

    global void execute(SchedulableContext sc) {
        StatusSync batch = new StatusSync();
        batch.queryStr = 'select Id, Name, Case_Number__c, Status__c, Case_Number__r.Request_Type__c from Ticket__c where Status__c != \'Closed\'';
        ID batchprocessid = Database.executeBatch(batch,10);
    }
} //This entire method is uncovered by test

Here is the test class I wrote:

@isTest
public class StatusSyncTest {
    
    @isTest
    static void testBatchStatus(){
        Case testCase = new Case();
            testCase.First_Name__c = 'test';
            testCase.Last_Name__c = 'tester';
            testCase.Request_Type__c = 'Provide My Info';
            testCase.SuppliedEmail = 'test@test.com';
            testCase.SuppliedPhone = '1115556200';
            testCase.Street__c = '123 fake avenue';
            testCase.City__c = 'Test City';
            testCase.State__c = 'AZ';
            testCase.Zip_Code__c = Decimal.ValueOf('90210');
            testCase.Status = 'New';
            insert testCase;
            system.assertNotEquals(null, testCase.Id);
            
            Ticket__c t = new Ticket__c();
            t.Name = 'ABCDXYZ';
            t.Case_Number__c = testCase.Id;
            t.Status__c = 'New';
            insert t;
        StatusSync batch = new StatusSync();
        Test.setMock(HttpCalloutMock.class, new ticketCalloutMock());
        Test.startTest();
        batch.queryStr = 'select Id, Name, Case_Number__c, Status__c, Case_Number__r.Request_Type__c from Ticket__c where Status__c != \'Closed\'';
        
        Database.executeBatch(batch);
        Test.stopTest();
    }
}
1
  • 2
    You're inserting a test Case record and test Ticket__c record, but the queryStr sent to the batch class is selecting data from Service_Ticket__c, which won't have any test data available to run the test batch execution on. Also, if your Ticket__c was supposed be a Service_Ticket__c, the Status__c shouldn't be Closed
    – Instread
    Sep 17, 2020 at 15:34

1 Answer 1

0

Use this to cover both scheduler and batch class

Test.starttest();

StatusSyncScheduler sh1 = new StatusSyncScheduler(); String sch = '0 0 23 * * ?'; System.schedule('Test schedule',sch, sh1);

Test.stopTest();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.