0

As described in the title, I'm getting 0 code coverage on the execute method.

enter image description here

Overall Description of Batch: For every case record that meets the criteria, create a new object (working_hours_log__c) and add it to the case.

Batch Class:

global class InsertWorkingHoursBatch implements Database.Batchable<sObject>, Database.Stateful {

    String query;
    global List<Id> casesIdsToAddHours;
    Id supportRecordType = Schema.SObjectType.Working_Hours_Log__c.getRecordTypeInfosByName().get('Support Record Type').getRecordTypeId();

    global InsertWorkingHoursBatch(List<Id>casesIdsToAddHours) {
        this.casesIdsToAddHours=casesIdsToAddHours;
        query = 'SELECT Id FROM Case WHERE Id IN:casesIdsToAddHours';
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Case> scope) {

        List <Working_Hours_Log__c> whList = new List<Working_Hours_Log__c>();
        Set <Working_Hours_Log__c> whSet = new Set<Working_Hours_Log__c>();
        for(Case c: scope) {
            Working_Hours_Log__c workLog = new Working_Hours_Log__c();
            workLog.Working_Hours_Minutes__c = 5;
            workLog.User__c = UserInfo.getUserId();
            workLog.RecordTypeId = supportRecordType;
            workLog.Case__c = c.Id;
            workLog.Date__c=Date.Today();
            workLog.Description__c='Mass';
            whList.add(workLog);
            whSet.add(workLog);
        }
        try {
            insert whList;
            } catch(Exception e) {
                System.debug('Error while trying to insert whList - MassInsertWorkingHoursBatch'+e.getMessage());
            }

        }

        global void finish(Database.BatchableContext BC) {

        }

    }

Batch Test Class:

@isTest
private class InsertWorkingHoursBatchTest {

    @isTest static void test_method_one() {
        List<Case> cases = new List<Case>();
        List<Id> casesId = new List<Id>();
        Id consumer = Schema.SObjectType.Case.getRecordTypeInfosByName().get('Consumer').getRecordTypeId();
        for(Integer i = 0; i < 10 ; i++) {
            Case c = new Case(Mass__c=true,RecordTypeId=consumer);
            cases.add(c);
            casesId.add(c.Id);
        }

        if(!cases.isEmpty()) {
            insert cases;
        }

        Test.startTest();
        InsertWorkingHoursBatch cls = new InsertWorkingHoursBatch(casesId);
        Database.executeBatch(cls,200);
        Test.stopTest();

    }

    @isTest static void test_method_two() {
        // Implement test code
    }

}
0

You're collecting the Case Id's before insert operation and passing it to batch.

In that scenario batch's start method doesn't yield any results in query, hence doesn't execute the batch.

Pseudo code to solve the issue:

for(Integer i = 0; i < 10 ; i++) {
    Case c = new Case(Mass__c=true,RecordTypeId=consumer);
    cases.add(c);
}

insert cases;

// verify and collect Case Id's after insert
for(Case c: cases) {
    // assert is important
    System.assertNotEquals(null, c.Id);
    casesId.add(c.Id);
}


Test.startTest();

InsertWorkingHoursBatch cls = new InsertWorkingHoursBatch(casesId);
Database.executeBatch(cls,200);

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.