Forking in Surefire Maven Plugin: is it safe to use forking?

1k views Asked by At

We have several options in Maven Surefire Plugin to describe forking. One of the options, forkCount, is explained like this:

Option to specify the number of VMs to fork in parallel in order to execute the tests. When terminated with "C", the number part is multiplied with the number of CPU cores. Floating point value are only accepted together with "C". If set to "0", no VM is forked and all tests are executed within the main process.

Basing on this I may guess that if a forked regime and used, and forks are reused (reuseForks=true) then the same JVM will be used for several tests. This means, if I have loaded some class into memory, the static members of that class can be reused in some other test and fail it unexpectedly.

Is my understanding correct?

1

There are 1 answers

0
approxiblue On BEST ANSWER

You're right. Tests with static elements are not thread-safe and should be excluded from parallel execution:

If the Suite or Parameterized is annotated with @NotThreadSafe, the suite classes are executed in single thread. You can also annotate individual test class referenced by Suite, and the other unannotated test classes in the Suite can be subject to run in parallel.

Note: As designed by JUnit runners, the static methods annotated with @BeforeClass and @AfterClass are called in parent thread. Assign classes to the @NotThreadSafe Suite to prevent from this trouble.