testing - 如何避免在测试期间收到 "Too many future calls"?

标签 testing salesforce apex-code

我的生产环境现在有很多触发器和类。这些都很好用,并且都在正常运行。但是,我无法部署几个新触发器,因为测试类调用了太多 future 的方法。我向您保证,我的代码被批量化,以便每次运行只调用一次 future 的方法。但是,当通过 IDE 部署触发器时,每个测试都会运行,因此 future 的调用会运行太多次。

我已经尝试在我 future 的所有调用周围放置一个 try/catch,希望如果它确实达到了限制,它只会路由到 catch 方法。尽管出现相同的错误,但它仍然无法部署。

我正在进行的主要 future 电话只能通过一个类进行引用。这是对我的网站执行 ping 操作的 HTTP 调用。

除了完全重做所有测试类之外,是否有任何方法可以避免此限制?正如您在下面看到的,多余的 future 调用发生在 (默认) 而不是特定触发器上。

10:39:15.617|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 85 out of 100 ******* CLOSE TO LIMIT
Number of query rows: 1474 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 19 out of 150
Number of DML rows: 23 out of 10000
Number of script statements: 2370 out of 200000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 11 out of 10 ******* CLOSE TO LIMIT

最佳答案

如果不查看具体代码,很难判断是什么路径导致您在测试期间达到 future 调用限制。值得探讨为什么当前部署中的组件会达到 future 方法的限制。

如果您的目标是让测试通过,您可以使用 Test.isRunningTest() 的组合, Limits.getFutureCalls()Limits.getLimitFutureCalls()这样 future 的方法就不会在测试期间被调用,否则它们会导致超出限制。

例如

 if(Test.isRunningTest() && Limits.getFutureCalls() >= Limits.getLimitFutureCalls()) {
     system.debug(LoggingLevel.Error, 'Future method limit reached. Skipping...');
 } else {
     callTheFutureMethod();
 }

关于testing - 如何避免在测试期间收到 "Too many future calls"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13128253/

相关文章:

android - 如何用JUNIT测试ui应用程序android并截图

testing - jmeter 中超出了 gc 内存开销

perl - 这是测试 Perl 代码的好方法吗?

python - 使用 simple-salesforce python 上传多个文件

testing - 在集成测试期间如何运行服务器(进程)?

salesforce - 检查 Salesforce 中的空白日期字段

Salesforce(apex) 使用Where toLowerCase 进行查询选择

twitter - 如何从托管在另一个云中的应用程序使用 Twitter 搜索 API?

salesforce - Apex 测试类 - System.NullPointerException : Attempt to de-reference a null object

java - 从 Java 访问 Salesforce Apex Soap Web 服务