n00b
n00b

Reputation: 6330

JUnit - Do static classes maintain state between test classes?

I have a unit test class which when ran individually passes. When I run it with all the other tests within the package it fails with the failure being that an independent static class has not been initialized. This independent static class is used by other classes and so it seems that its state is maintained between tests.

Is my observation correct or is something else happening? Also it would be good if someone could provide a link or something as reference.

Thanks!

Upvotes: 12

Views: 8106

Answers (2)

Stephen C
Stephen C

Reputation: 718758

This independent static class is used by other classes and so it seems that its state is maintained between tests.

Yes. That is what will happen. And this is just one of the reasons that statics are awkward.

Static fields exist for the lifetime of the classes that define them, and that generally means for the lifetime of the JVM. I tried to find a place in the JLS where this is stated explicitly. The closest I could find was JLS 8.3.1.1 which says:

"If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4)."

Elsewhere the JLS says that a class is initialized only once.

The exception is when a class gets unloaded, but that won't happen here. Or at least, not with normal / default JUnit framework behaviour. (But it can be done: see Using different classloaders for different JUnit tests?)

And in case you are worried, there isn't any JUnit "magic" to reset the statics to their initial state (however you defines that). It is too complicated (and horrible) to contemplate implementing that.

Upvotes: 15

yshavit
yshavit

Reputation: 43391

Methods have no state (except while a given method is running, of course), so none is saved between invocations — even for static methods..

Any static field saves its state for the duration of the JVM's execution (unless code changes its value, of course). JUnit uses one JVM for all of its tests, so, yes, static fields save state between tests.

That's one of the biggest reason that people recommend not using static fields where it's possible to avoid them: it reduces the amount of global state you have to worry about, and thus makes it much easier to reason about tests.

Upvotes: 6

Related Questions