8

If you have a test class with multiple test methods in it, and some static member along with a static initializer, then

1) Test methods will run in no particular order (i'm sure you knew this one already).

2) Governor limits are reset for every test method.

3) Governor limits are shared by the test method and static initializer.

4) Static members are nulled out for each test method being run.

5) Static initializer runs for every test method, instead of just once for the class.

So question for #4 and #5 - is it expected behavior or a bug?

1 Answer 1

6

I can't find a documentation link, but I believe this is expected behavior. Unit Tests should be completely independent, so 4 & 5 must be true. Consider that any test method which changes a static value could affect another test that also uses that static value. And because of #1, you can't predict how they will interact. In order to prevent unit tests from affecting each other, each test must be run in its own transaction; therefore static members will always start as null and initializers must run each time.

2
  • I agree with Jason. Imagine testing a cross app utility class that has some static methods and static variables. They'd run separately in normal conditions, but during testing they'd interact in unpredictable ways.
    – drakored
    Jan 23, 2014 at 22:40
  • I think there is an exception though, when multiple test methods are called from a single test method, statics are shared. Jan 23, 2014 at 22:43

You must log in to answer this question.

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