+1 vote
159 views
by (163k points)

I am trying to write a test method for my batch class so I can deploy it, but I am getting this error.

System.UnexpectedException: Start did not return a valid iterable object.

The batch class looks like this:

public class DataLoadBatch implements Database.Batchable<SObject>, Database.Stateful,  Database.AllowsCallouts{

    public static AppCustomSettings__c AppCustomSettings= AppCustomSettings__c .getInstance('API Integration');

    public List<Contact> start(Database.BatchableContext bc) {
        List<Contact> contacts = ExternalAPI.getContactIdsToLoad(AppCustomSettings.API_Key__c);
        return contacts;
    }

    public void execute(Database.BatchableContext bc, List<Contact> scope) {
        try {
            update scope;
        }catch (DmlException e) {
            System.debug('DML Error---------> ' + e.getMessage());
        }
    }

    public void finish(Database.BatchableContext BC) {
    }


}

The test class looks like this so far:

@IsTest
static void testContactIDLoad() {
   AppCustomSettings__c AppCustomSettings= new AppCustomSettings__c();
   AppCustomSettings = 'API Integration';
   AppCustomSettings.Base_URL__c = 'https://example.com/api/v1';
   AppCustomSettings.API_Key__c = '12345678xxxxxapikey';
   insert AppCustomSettings;

   Test.setMock(HttpCalloutMock.class, new ContactHTTPResponseMock());

   Test.startTest();
    ContactLoadBatch clBatch = new ContactLoadBatch();
    Database.executeBatch(clBatch);
   Test.stopTest();
}

To resolve, I tried to create a list of contact records and pass that into the database.execute() method as as the 2nd parameter, but I then got an error indicating "method does not exist or incorrect signature: void executeBatch(ContactLoadBatch,List) from the type Database"

How can I resolve this error?

1 Answer

+1 vote
by (163k points)
 
Best answer

Your mock needs to return a list of records, or, failing that, return something that will translate to an empty list (although this won't get your execute method called). You could call the execute method directly, but you need to remember it needs two parameters:

clBatch.execute(null, contactList);

You shouldn't do this if you can help it, though. It'd be better to fix your mock so that it returns data.

Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
You can donate any amount for Orphans village using following QR Code.
...