JUnit tests influence each other

2019-05-27 04:05发布

问题:

I'm working with a lot of legacy code. There was a JUnit-TestSuite to begin with. When running all tests with gradle, they failed. When running the test in IntelliJ, they worked. We configured gradle to use the test suite.

Now someone reported tests working locally without gradle, but not with gradle. It's time we fix this mess.

Is there a smart way to figure out which test leaves some configuration behind or which tests relies on the other tests?

回答1:

The most likely cause of this "bleed" from one test into another is mutable static values. By default, all tests are run by the same JVM so a static variable which is "mutated" by one test will be "dirty" in another test.

Mutable statics are evil! I'm working on a codebase currently with mutable statics everywhere and it's a mess. If possible you should refactor to use dependency injection and store mutable state in instances and not statics.

As a workaround, to get the tests running in gradle you could use Test.forkEvery to use a separate JVM per test so static variables will be "clean" for each test invocation. Eg

test {
    forkEvery = 1
}