【问题标题】:No more than one executeBatch can be called from within a testmethod在一个测试方法中只能调用一个 executeBatch
【发布时间】:2020-07-25 16:17:44
【问题描述】:

如何在测试类中修复以下错误消息

            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.

请注意,已经尝试按照此处https://help.salesforce.com/articleView?id=000330685&type=1&mode=1 的建议添加限制 200,但没有成功

我的顶级课程是

            global class ERTExtract255BatchClass implements Database.Batchable<sObject> {
                global Database.QueryLocator start(Database.BatchableContext bc) {
                    return Database.getQueryLocator(
                'SELECT ID,Description,Case_Desc_255__c FROM Case' 
                    );
                }
                global void execute(Database.BatchableContext bc, List<Case> scope){
                // process each batch of record
                List<Case> lstCase = new List<Case>();       
                for (Case cas : scope) {
                string Strdesc = cas.Description ; 
            if(Strdesc.length()>255){
                cas.Case_Desc_255__c = cas.Description.Left(255);
                lstCase.add(cas);  
            }
                }   
            update lstCase;
            }   
            global void finish(Database.BatchableContext bc){

            }   
 
            }

我的测试课是

            @isTest(SeeAllData=false)
            public class testERTExtract255BatchClass {

            @IsTest
            static void testBatchJob(){
              
               List<Case> cases = new List<Case>();
               for (integer i =0;i<300;i++)
               {
                    Case c = new Case();
                    c.Description = 'aaaaaa'.rightPad(255,'b');
                    c.status = 'new';
                    c.Subject = 'test';
                    //add other mandatory fields 
                    cases.add(c);
                }

                insert cases;

                Test.startTest();
                Database.executeBatch(new ERTExtract255BatchClass());
                Test.stopTest();


                Case Strcase = [Select id,Case_Desc_255__c from Case];
                System.assertEquals(Strcase.Case_Desc_255__c.length(),255);

                    }
            }

【问题讨论】:

    标签: salesforce apex salesforce-lightning


    【解决方案1】:

    您需要for (integer i =0;i&lt;300;i++) 的任何特殊原因? 如果你赚 200 或更少,它应该很适合“单元测试中的 1 个 execute() 调用”。

    或者您可以做的是将可选参数传递给Database.executeBatch 方法。 (在该文档中,您可以看到默认值为 200)。所以如果你去Database.executeBatch(new ERTExtract255BatchClass(), cases.size()); 应该没问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 2023-04-01
      • 2020-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多