You need to sign in to do that
Don't have an account?

Trigger Static Recursive Variables in Test Classes
Hi,
I'm using a static recursive variables for my triggers and I am encountering an issue when testing. I have 2 triggers. One on a custom object called procedure and another on custom object revenue that updates a procedure when inserted.
So in my test class I insert a procedure record, the first trigger runs and at the end sets the recursive variable hasRun = True.
Now when I go an insert my Revenue record, I want it to run through the procedure trigger again, but hasRun still equals to True? Do you need to reset static variables like that in test cases because test classes are considered a single execution state?
It will remain set within the same test method. It is considered the same context.
It will reset to default in between test methods.
All Answers
No, I don't think it will be needed to be reset in test classes.
You can share if you face any issue regarding this.
That was my first assumption as well,
I just debugged the static variable through my test method and it doesn't reset it remains the same through the entire test method.
I declare my static variable in a class as such:
public class DataUtil {
public static Boolean HasRun = False;
}
This is the execution in the test method:
@isTest static void untiTest() {
insert Procedures
//Procedure Trigger fires and sets static variable HasRun = TRUE
insert Revenue
//Revenue trigger fires and updates procedures. HasRun blocks the trigger from running, when you would assume it would //reset because of a new DML operation
}
Not really sure what to take from this, whether I am doing something wrong in the test method or with the recursive variable.
It will remain set within the same test method. It is considered the same context.
It will reset to default in between test methods.