function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anan 

Testing trigger that calls @future method

I have a trigger that fires when contact object is updated and updates the related user's isActive checkbox when the spesific checkbox in the contact has been changed.

 

Because the user and contact cannot be updated in the same transaction contact trigger calls the future method that updates the user's isActive field.  This is simplified trigger code.

 

 

trigger InsertUpdateContactTrigger on Contact (after update) {
List<String> contactsToUpdate = new List<String>();
for (Contact c : trigger.new){
if(trigger.newMap.get(c.Id).IsActive__c != c.IsActive__c){
contactsToUpdate.add(c.Id);
}
}

AsyncUserUpdateHelper.updateContactUserInfo(contactsToUpdate);
}

This works fine in practice in other word when I update the contact in UI trigger fires and user is updated. I have also written a testMethod for trigger and test passes when I run it in Eclipse IDE. I am also able to deploy the trigger in production through IDE and during deployment tests run successfully. The problem is that when I try to 'Run Test' or 'Run All Tests' in Setup->Develop->Apex Classes tests fail because of MIXED_DML_OPERATION error from the row 4.

1 private static testMethod void testUpdateContact(){
2 //QUERY CONTACTS THAT HAVE PORTAL USER
3 Test.startTest();
4 update portalContacts;
5 Test.stopTest();
6 //ASSERTIONS
7 }

Does using startTest and stopTest case the both trigger and future method to run in same transaction and if so how can I test the trigger that calls @future method? And why the tests run fine when deploying the through IDE but not when running them directly from salesforce?

bob_buzzardbob_buzzard

Check out this thread - sounds like you are hitting a similar problem.

 

anan

Thanks! I think that explained the problem.

 

So apparently there is no way to test the trigger that fires a future method. I think only way to get needed test coverage is to test @future method separately and to do a dummy test method for trigger. With dummy testMethod I mean test method that executes the trigger code but does not cause the @future method to do update on user.

 

Please correct me if there is better solution for the problem.

Swap50Swap50

I'm facing the same issue.

Any work around for this?

I have a custom object which on insert, creates a GropuMember.

I used future annotation to make this work through UI.

But not able to complete the test coverage. Getting "Mixed DML Operation error".

Sreejesh nairSreejesh nair

What I ended up doing was something like this.

 

    static testMethod void testForFutureCallFromTrigger() {
        DummyObject__c testDummyObject = createDummyObject();
        User testUser = createaTestUser();
        testDummyObject.user__c = testUser.Id;
        insert testDummyObject;     
        testDummyObject.blah__c = 102;
        update testDummyObject;   
        System.assertEquals(1,Limits.getFutureCalls());
    }

 

So what I am doing here is asserting the Future call happened...

 

Testing the Future call in the apex class is another story..Like others mentioned it can be a separate test method in an another testClass if needed.