0 votes
2 views
by

I have a batch class and I wrote test class for the same. The test class is throwing an error at Date Type. Can someone help me how to fix the issue.

Batch Class

public without sharing class EtcObjectBatchProcess implements Database.Batchable<SObject> {
    public String action;
    public Date startDt;
    public Date endDt;
    public EtcObjectBatchProcess(String operation, Date startDate, Date endDate) {
        action = operation;
        startDt = startDate;
        endDt = endDate;
    }
    public Database.QueryLocator start(Database.BatchableContext bc) {        
        String query = '';
        if(!Test.isRunningTest()) {
            query = 'SELECT Id, CreatedDate, Qualification__c FROM Registration__c';
        }
        else {
            query = 'SELECT Id, CreatedDate, Qualification__c FROM Registration__c '+
                    + 'WHERE CreatedDate >=:' + startDt + 'AND CreatedDate <=:' + endDt;
        }
        return Database.getQueryLocator(query);
    }
    *public void execute(Database.BatchableContext BC, List<Registration__c> records) {
        
        List<Registration__c> deleteList = new List<Registration__c>();
        List<Registration__c> updatedList = new List<Registration__c>();
        for(Registration__c rec : records) {
            if(action == 'delete') {
                deleteList.add(rec);
            }
            else if(action == 'reprocess') {
                if(rec.Qualification__c == '' || rec.Qualification__c == NULL) {
                    rec.Qualification__c = 'MBA';
                    updatedList.add(rec);
                }
            }
        }
        if(deleteList.size() > 0) {
            delete deleteList;
        }
        if(updatedList.size() > 0) {
            update updatedList;
        }
    }
    public void finish(Database.BatchableContext bc) {
    }
}*

Test Class

@isTest
public class EtcObjectBatchProcessTest {
    public static testMethod void method1() {
        List<Registration__c> regList = new List<Registration__c>();
        regList.add(new Registration__c(First_Name__c = 'Test', Qualification__c = ''));
        regList.add(new Registration__c(First_Name__c = 'Test1', Qualification__c = ''));
        insert regList;
        
        Test.startTest();
        Database.executeBatch(new EtcObjectBatchProcess('reprocess', Date.today(), Date.today().addDays(-10)));
        Test.stopTest();
    }
}

I am running the batch based on parameters form the Vf Page and the highlighted lines are not getting covered. Thanks in Advance...

1 Answer

0 votes
by
 
Best answer

I think you meant:

if(Test.isRunningTest()) {

instead of:

if(!Test.isRunningTest()) {

Please be aware that this is definitely not considered a best practice.

In addition, your query is malformed:

+ 'WHERE CreatedDate >=:' + startDt + 'AND CreatedDate <=:' + endDt;

Ends up rendering as something like:

WHERE CreatedDate >=:2021-04-16AND CreatedDate <=:2021-04-06

Which is an invalid SOQL statement, both because of the colons and the lack of a space in the query.

Either use proper binding, or format the dates correctly (not both):

+ 'WHERE CreatedDate >=:startDt AND CreatedDate <=:endDt';

Or:

+ 'WHERE CreatedDate >= '+startDt+' AND CreatedDate <= '+endDt;

Note also that your test has the filters backwards. The end date occurs before the start date, so no records will match this query.

WHERE CreatedDate >= 2021-04-16 AND CreatedDate <= 2021-04-16

This will never be true, so no records will match. This is another reason why your query would return no results, and execute won't be called.


Your code is unnecessarily complicated, and should be fixed:

public without sharing class EtcObjectBatchProcess implements Database.Batchable<SObject> {
    String action;
    Date startDt;
    Date endDt;
    public EtcObjectBatchProcess(String operation, Date startDate, Date endDate) {
        action = operation;
        startDt = startDate;
        endDt = endDate;
    }
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator([
            SELECT Qualification__c 
            FROM Registration__c
            WHERE CreatedDate >= :startDt AND CreatedDate <= endDt
        ]);
    }
    public void execute(Database.BatchableContext BC, List<Registration__c> records) {
        switch on action {
            when 'delete' {
                delete records;
            }
            when 'reprocess' {
                Registration__c[] updates = new Registration__c[0];
                for(Registration__c record: records) {
                    if(record.Qualification__c == null) {
                        updates.add(record);
                        record.Qualification__c = 'MBA';
                    }
                }
                update updates;
            }
        }
    }
    public void finish(Database.BatchableContext bc) {
    }
}

And your unit test can be fixed:

@isTest
public class EtcObjectBatchProcessTest {
    public static testMethod void method1() {
        List<Registration__c> regList = new List<Registration__c>();
        regList.add(new Registration__c(First_Name__c = 'Test', Qualification__c = ''));
        regList.add(new Registration__c(First_Name__c = 'Test1', Qualification__c = ''));
        insert regList;
        
        Test.startTest();
        Database.executeBatch(new EtcObjectBatchProcess('reprocess', Date.today().addDays(-10), Date.today()));
        Test.stopTest();
    }
}
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...