0

I have some problems with the SandboxPostCopy interface test class. My goal is to clean some sensitive data on some Object after a refresh. The class is this:

global class CleanSandbox implements SandboxPostCopy {

global void runApexClass(SandboxContext context) {

    CleanAccount ca = new CleanAccount();
    CleanUser cu = new CleanUser();
    CleanCase cc = new CleanCase();
    CleanOpportunity co = new CleanOpportunity();
    CleanServiceContract csc = new CleanServiceContract();

    ID batchprocessid = Database.executeBatch(ca, 200);
    batchprocessid = Database.executeBatch(cu, 200);
    batchprocessid = Database.executeBatch(cc, 200);
    batchprocessid = Database.executeBatch(co, 200);
    batchprocessid = Database.executeBatch(csc, 200);
}

}

It executes some batches in a row and when I run this test class:

    @isTest
private class CleanSandboxTest {
@testSetup
public static void initialization(){       
    Account acc = new Account(Name='Test PA', BillingCity='Test',BillingState='RM', BillingStreet='TestStreet',BillingPostalCode='00000', Type = 'Customer');
    insert acc;

    Profile p = [SELECT Id FROM Profile WHERE Name='Support']; 
    User u = new User(Alias = 'alex', Email='usermat1@testorg.com', 
                      EmailEncodingKey='UTF-8', LastName='Alex', LanguageLocaleKey='en_US', 
                      LocaleSidKey='en_US', ProfileId = p.Id, 
                      TimeZoneSidKey='America/Los_Angeles', UserName='usermat1@testorg.com');
    insert u;
}
@isTest
static void testSandboxPostCopyScript() {
    CleanSandbox cs = new CleanSandbox();
    Test.startTest();
    Test.testSandboxPostCopyScript(cs, '01D8E01234B6BZA', '01D8E01234B6BZA', 'sandboxName');        
    Test.stopTest();

}
}

It throws this exception:

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.

How could I resolve? Thanks in advance.

  • Unless you're refreshing a partial copy or full copy sandbox, there shouldn't be anything for you to "clean" (except for User records, which is copied over even to developer sandboxes). Given that, do you really need to use batch apex here? – Derek F Oct 12 '17 at 13:16
  • Yes, when I refresh I always do a full copy of the production environment and some real records details of the clients mustn't shown in the sandbox. – DarkSkull Oct 12 '17 at 13:19
0

As the error clearly says we can't process more than 200 records in test class for the batch apex. In your use case there are multiple batch processing in single transaction which are not allowed in Test classes. So you need to use Test.isrunningTest() here.

As per Salesforce docs

What you have to consider is using the Test.isRunningTest to bypass the code starting the second job in this context. Meaning that you will have to test your second batch job in a separate test to get coverage and assert behaviour. In doing so you will obviously have to reproduce manually in the test code the state in the database the second job expects. Not ideal, but should work.

So you need to do something like

ID batchprocessid = Database.executeBatch(ca, 200);
if(!Test.isrunningTest()) {
    batchprocessid = Database.executeBatch(cu, 200);
    batchprocessid = Database.executeBatch(cc, 200);
    batchprocessid = Database.executeBatch(co, 200);
    batchprocessid = Database.executeBatch(csc, 200);
}

Error "System.UnexpectedException: No more than one executeBatch can be called from within a testmethod"

  • So, is it not possible to test all these classes together (CleanAccount, CleanUser, CleanCase, CleanOpportunity, CleanServiceContract)? – DarkSkull Oct 12 '17 at 10:59
  • @DarkSkull unfortunately no. You can't test them in single method. – Tushar Sharma Oct 12 '17 at 11:11

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.