Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven surefire: when are we forced to set reuseForks=false?

In my project when I set reuseForks=true then i have to increase the forkCount to number of test classes. Otherwise, It is throwing illegalargument exception. Also, If I set reuseForks=false then it also work fine.

Currently I have following configuration because number of test classes are less than 10.

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.18.1</version>
            <configuration>
                <reuseForks>true</reuseForks>
                <forkCount>10</forkCount>
            </configuration>
        </plugin>

How can I keep reuseFork=true and forkCount=1.

EDIT: StackTrace on reuseFork=true and forkCount=1

    checkForReturnEventsPresent on checkForReturnEventsPresent(com.eras.senders.OMSReturnEventDataSenderTest)(com.eras.senders.OMSReturnEventDataSenderTest)  Time elapsed: 0.014 sec  <<< FAILURE!
java.lang.IllegalArgumentException: null
    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:115)
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:212)
    at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:108)
    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:111)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
like image 917
Vivek Baranwal Avatar asked Jan 08 '16 19:01

Vivek Baranwal


People also ask

What is reuseForks?

The parameter reuseForks is used to define whether to terminate the spawned process after one test class and to create a new process for the next test in line ( reuseForks=false ), or whether to reuse the processes to execute the next tests ( reuseForks=true ).

How do I disable surefire report in Maven?

1 Answer. Show activity on this post. https://maven.apache.org/surefire/maven-surefire-report-plugin/report-mojo.html#skipSurefireReport. this code will skip the surefire tests.

What is forked process in Maven?

Pinging forked JVM Simply the mechanism checks the Maven PID is still alive and it is not reused by OS in another application. If Maven process has died, the forked JVM is killed.

What is argLine in surefire?

Since the Version 2.17 using an alternate syntax for argLine , @{... } allows late replacement of properties when the plugin is executed, so properties that have been modified by other plugins will be picked up correctly.


1 Answers

In my project when I set reuseForks=true then i have to increase the forkCount to number of test classes. Otherwise, It is throwing illegalargument exception.

From the stack trace, it appears something inside your code is causing the exception to be thrown, but only when forks are reused. This implies that the different tests are somehow not isolated from each other, and therefore running one after the other within the same test process violates some assumptions. For example, perhaps one test initializes some global state like a singleton, and then that global state isn't correct for the next test run within that process.

Also, If I set reuseForks=false then it also work fine.

By setting reuseForks to false, any problems related to mutating global state like this get bypassed. The process gets torn down at the end of the test run, and a new process with fresh state gets started for the next test run.

At this point, the path forward is highly dependent on the specifics of your codebase and how its tests are implemented. I see 2 options:

  1. Debug your tests looking for problems of global state like I described. Attaching a breakpoint right before this exception would probably give you a strong hint.
  2. If you already know that the codebase relies on some global state, and it isn't feasible to change that code quickly, then just stick with reuseForks set to false. In many cases, the extra process teardown and startup won't cause a noticeable performance impact for your overall test run.
like image 139
Chris Nauroth Avatar answered Nov 15 '22 02:11

Chris Nauroth