0

According to my functionality: insert and update calls a trigger that invokes future methods. In my test case, I need to test the update functionality. In order to test the update functionality, I need to first insert the account with appropriate fields. Because of this, the 'after insert' trigger invokes a future method. After this, I am making an 'update' that invokes another future method. Since in one test case , there will be two future methods that will be invoked as a result of 'after insert' and 'update' trigger , How do I test this ? The below function throws an error that seems like invoking 2 future inside a single test case didn't work properly.

Please let me know how I can test this , where am I making mistake ?

Thanking you in advance.

@isTest static void updateOrd(){
    Account parentAcc = new Account(name = 'Parent Test Account', 
                                    External_Id__c = 'abcd123ee');
    Test.startTest();
    Test.setMock(HttpCalloutMock.class, new MockHttpChildResponseGenerator()); 
    Account childAcc = new Account(name = 'Test Account', Active__c = true');
    insert childAcc;
    childAcc.Active__c = false;
    Test.setMock(HttpCalloutMock.class, new MockHttpDeactivateResponseGenerator()); 
    update childAcc;
    Test.stopTest();
    Account updatedChildAcnt = [SELECT External_Id__c from Account where External_Id__c =:'abcd123ee'];
    System.assertNotEquals(updatedChildAcnt.Active__c, false);
}

1 Answer 1

0

I could see this being an issue with your testing approach more than anything. Do you want to test the insert, or do you want to test the update? You're currently doing both inside of Test.startTest() and Test.stopTest() which is where you cause the error to happen. If you move the insert outside of start and stop test you can start your test under new transaction limits. Most likely you want two unit tests here, one for insert and another for update.

@isTest static void updateOrd(){
    Account parentAcc = new Account(name = 'Parent Test Account', 
                                    External_Id__c = 'abcd123ee');
    Test.setMock(HttpCalloutMock.class, new MockHttpChildResponseGenerator()); 
    Account childAcc = new Account(name = 'Test Account', Active__c = true);
    insert childAcc;
    
    /* transaction limits reset, controlled by Test.startTest() and Test.stopTest() */
    Test.startTest();
    
    childAcc.Active__c = false;
    Test.setMock(HttpCalloutMock.class, new MockHttpDeactivateResponseGenerator()); 
    update childAcc;

    Test.stopTest();

    Account updatedChildAcnt = [SELECT External_Id__c from Account where External_Id__c =:'abcd123ee'];
    System.assertNotEquals(updatedChildAcnt.Active__c, false);
}
3
  • Please correct me if I am wrong , I only want to test update ! But in order to update , I need to do initial set up of creating an account. But my "insert" also contains a trigger which would invoke future method. I thought that "future" methods should always be inside Test.start() and Test.stop() which is why I kept both insert and update inside Test.start().
    – M Dhanapal
    Aug 20, 2021 at 13:51
  • I don't believe it's strictly necessary that future methods have to be invoked inside of start and stop. If this approach doesn't work, leave a comment back. You may also be able to use a Test Setup method instead to separate the transactions within your unit tests.
    – nbrown
    Aug 20, 2021 at 14:13
  • 1
    I would suggest that you remove the invocation of future methods from a trigger since this isn't going to work when you have the triggers invoked from asynchronous contexts. Take a look at this previous Q&A on the subject.
    – Phil W
    Aug 20, 2021 at 16:58

You must log in to answer this question.

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