Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JUnit test *methods* in isolation

Is maven-surefire-plugin able to run all JUnit test methods in isolation? I.e., is it able to fork a JVM for each test method rather than for each test class?

The deprecated option

<forkMode>pertest</forkMode>

and the current

<forkCount>1</forkCount>
<reuseForks>false</reuseForks>

seem to only fork for each test class.

PS: Test methods should be independent and therefore no one should need to run each one on a new JVM (and not to say that it would be very expensive). But I was wondering whether there is such option or not.

like image 701
josecampos Avatar asked Aug 30 '17 22:08

josecampos


1 Answers

The Maven Surefire Plugin supports this configuration:

<parallel>methods</parallel>

For JUnit versions >= 4.7

For example:

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
      <parallel>methods</parallel>
      <threadCount>10</threadCount>
    </configuration>
  </plugin>

More details here.

Edit 1 based on your comment:

I would like to run each test method on a new JVM, and not on another thread on the same JVM

When using reuseForks=true and a forkCount > 1, test classes are handed over to the forked process one-by-one. So, with parallel=methods classes are executed in forkCount concurrent processes, each of these processes uses a threadCount number threads to execute the methods of one class in parallel.

So, it looks like surefire does not support your use case.

FWIW, spawning a JVM for every test method sounds like it could get expensive. Are you perhaps dealing with an issue where test methods have unwanted side effects within a JVM (maybe they set System properties or change statics) and these side effects cannot be isolated? If so, perhaps revisting the tests to eliminate these side effects might be more desireable than implementing a bespoke and potentially expensive test runner?

like image 88
glytching Avatar answered Sep 22 '22 09:09

glytching