I can't understand why I am getting 0 from Limits.getEmailInvocations(); that is run after Test.stoptest().

The method which is sending emails is @future annotated and run from trigger handler on afterUpdate.

The code:

if (mails.size() > 0) {
    System.debug (LoggingLevel.INFO, 'EmailServiceReportOnWOorWOLICreation emails sent: ' + mails.size());

Test Class:

    Integer invocations = Limits.getEmailInvocations();
    System.assertEquals(2, invocations);

Debug Logs: 15:48:49.511 (87578825150)|USER_DEBUG|[265]|INFO|EmailServiceReportOnWOorWOLICreation emails sent: 2

15:48:50.590 (88810978768)|SYSTEM_METHOD_EXIT|[266]|Messaging.sendEmail(List<Messaging.Email>)

15:48:50.590 (88811016601)|METHOD_EXIT|[102]|01p1j000007FhVC|EmailServiceReportOnWOorWOLICreation.EmailServiceReport_sendEmails(Map<Id,WorkOrder>, Map<Id,WorkOrderLineItem>, Map<Id,String>)

15:48:50.590 (88811041063)|METHOD_EXIT|[25]|01p1j000007FhVC|EmailServiceReportOnWOorWOLICreation.EmailServiceReport_filterRecords(Map<Id,ANY>, Map<Id,ANY>, Schema.SObjectType)

15:48:50.590 (88811053585)|SYSTEM_MODE_EXIT|false

15:48:50.590 (88811097366)|CODE_UNIT_FINISHED|EmailServiceReportOnWOorWOLICreation.EmailServiceReport_Future(String, String)

15:48:50.590 (88813690012)|METHOD_EXIT|[99]||System.Test.stopTest()

15:48:50.590 (88813761151)|SYSTEM_METHOD_ENTRY|[100]|Limit.getEmailInvocations()

15:48:50.590 (88813844031)|SYSTEM_METHOD_EXIT|[100]|Limit.getEmailInvocations()

15:48:50.590 (88813880727)|SYSTEM_METHOD_ENTRY|[102]|System.assertEquals(ANY, ANY)

15:48:50.590 (88813935742)|EXCEPTION_THROWN|[102]|System.AssertException: Assertion Failed: Expected: 2, Actual: 0

  • To get 100% coverage (and verify it works), just include a static variable in the class being tested, and then modify it when your future method is called. Since static methods are not reset when Test.stopTest is called, you can verify the limits work. I linked a couple of questions that should help you get where you're trying to go.
    – sfdcfox
    Commented Jun 29, 2021 at 1:14

1 Answer 1


The limits get reset when you call Test.stopTest(). If you want to test limits consumption of a @future method, you would need a synchronous version which it calls in turn.

public static void doStuff()
    // actual logic here
public static void doStuffAsync()
  • I also tried this: Integer invocations = Limits.getEmailInvocations(); System.debug('Email Invocation: ' + invocations); Test.stopTest(); System.assertEquals(1, invocations); But value in debug logs is 0 still. Is there any other possibility to make assertion in test class that will check if email sending is triggered? I don't need limit value just any trick to make valid test with assert statment. Commented Jun 28, 2021 at 14:37
  • No. The asynchronous code won't run until you call Test.stopTest().
    – Adrian Larson
    Commented Jun 28, 2021 at 14:53
  • So following your suggestion I need to add statement if(Test.isRunningTest()) {doStuff} else {doAsyncStuff}. Will check that tommorow as i just finished my work for today. Commented Jun 28, 2021 at 15:07
  • Not even close to what I am suggesting. All I am saying is move your logic to the body of a method you can call synchronously, then call that synchronous method from your future.
    – Adrian Larson
    Commented Jun 28, 2021 at 15:37

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