JUnit tests affect each other

I am working with a lot of legacy code. For starters, there was JUnit-TestSuite. When running all tests with gradle, they did not run. When running the test in IntelliJ, they worked. We have configured gradle to use a test suite.

Now someone reported tests working locally without gradle, but not with gradle. This time we are fixing this disorder.

Is there a sane way to figure out which test leaves some configuration, or which tests depend on other tests?

+2


source to share


1 answer


The most likely reason for this "expiration" from one test to another is changed static values. By default, all tests are run by the same JVM, so a static variable that is mutated by one test will be dirty in another test.

Variable static is evil! I am working on a codebase that currently uses mutable statics and this is a mess. If possible, you should refactor your use of dependency injection and keep mutable state in cases, not statics.



As a workaround for running tests in gradle, you can use Test.forkEvery to use a separate JVM per test so that the static variables are "clean" for each invocation call. For example,

test {
    forkEvery = 1
}

      

+3


source







All Articles