0

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

0

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.

2
  • Changing the mock to return data in theory seems like it should work @sfdcfox, but the mock has a return type of HttpResponse which does not allow me to return a list of contacts. I tried changing the return type to List<Contact>, but got an error: Class must implement the method: System.HttpResponse System.HttpCalloutMock.respond(System.HttpRequest) Are you suggesting return the JSON in the mock and then based on that get it into a list of contacts in the calling code? Commented Oct 20, 2020 at 20:35
  • @user1669296 No, the mock needs to return data that would then presumably be deserialized by ExternalAPI.getContactIdsToLoad.
    – sfdcfox
    Commented Oct 20, 2020 at 20:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .