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?