1

I'm writing some test cases for a custom class that interacts with a custom LWC that requires fields from SF contacts. I'm attempting to load test data from a static resource using Test.loadData as shown below:

@isTest(SeeAllData=false)
public static void getClientVaultTestOne(){
    List<sObject> testContacts = Test.loadData(Contact.getSObjectType(),'TestData');
    System.assert(testContacts.size() == 7, 'Error Loading Test Data');
    Contact testContact = null;

    for(sObject obj : testContacts){
        Contact c = (Contact)obj;
        if(c.FirstName.contains('Jones')){
            testContact = c;
            break;
        }
    }

    System.debug('Test Contact: ' + testContact);

    Test.startTest();
    Test.setMock(HttpCalloutMock.class, new ClientVaultCalloutMock());

    List<Map<String, Object>> clientFiles = DashboardController.getClientVault(testContact.Id);

    Boolean testValid = true;
    if(clientFiles.size() < 1){ testValid = false; }

    for(Map<String,Object> element : clientFiles){
        if(element.isEmpty()){
            testValid = false;
        }
    }

    System.assert(testValid, 'getClientVaultTestOne Failed');
    Test.stopTest();
}

But for some reason this isn't exactly working. Salesforce is giving me a weird Exception when I try to run this test:

System.UnexpectedException: No more than one executeBatch can be called from within a test method.  
Please make sure the iterable returned from your start method matches the batch size, resulting in one executeBatch invocation.
External entry point 3564

I haven't been able to find cases of this happening elsewhere on the internet, and I'm following Salesforce documentation to the best of my ability but this is what I'm greeted with.

Any ideas on what I should change?

1
  • Sounds like you have too many records in your resource.
    – Adrian Larson
    Commented Aug 16, 2022 at 15:23

1 Answer 1

0

The error means that an Apex Batch job is running during your test, and more than one batch of records is being processed (a test can only execute for a single batch of records).

Since your test isn't explicitly running any batch jobs, something else must be doing so. Without seeing all of the code I cannot tell for sure, but my guess is a trigger on Contact is kicking off a batch. See if you can track down what is running the batch and determine how to avoid it. This could involve inserting fewer test records, or setting a static variable in the trigger or batch to bypass the batch, or any of a number of other solutions. The key is to either avoid the batch running at all, or to reduce the number of records covered by the batch. Without seeing all code involved, I cannot offer a more concrete solution.

1
  • First thank you for your reply! I appreciate the help. Second, I can confirm that the static resource which I'm loading for this test data has only seven records, so it sounds like you're right on something else like a trigger running during a test. It's weird that Salesforce works like this to me but it is what it is I guess lol. Do you happen to know if there's something that can be done in Apex to prevent automations like this from running? Commented Aug 17, 2022 at 16:04

You must log in to answer this question.

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