JUnit + Maven + Parallel Test Execution Error
Asked Answered
S

3

9

I have a problem executing JUnit Tests in parallel when Using JUnit, Groovy, Spock and Maven. When executing them, I get the following after test are passed succesfully:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 18.362s
[INFO] Finished at: Wed Mar 20 15:14:25 CET 2013
[INFO] Final Memory: 16M/221M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.14:test (default-test) on project spock-webdriver: ExecutionException; nested exception is java.util.concurrent.ExecutionException: java.lang.RuntimeException: There was an error in the forked process
[ERROR] java.lang.NoSuchMethodError: org.apache.maven.surefire.common.junit4.JUnit4RunListener.rethrowAnyTestMechanismFailures(Lorg/junit/runner/Result;)V
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:129)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.lang.reflect.Method.invoke(Method.java:601)
[ERROR] at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208)
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:158)
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95)
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

When enabling to force using Junit 47 Provider in my pom.xml, I get that:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.271s
[INFO] Finished at: Wed Mar 20 15:31:42 CET 2013
[INFO] Final Memory: 17M/178M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.14:test (default-test) on project spock-webdriver: ExecutionException; nested exception is java.util.concurrent.ExecutionException: java.lang.RuntimeException: There was an error in the forked process
[ERROR] java.lang.NoSuchMethodError: org.apache.maven.surefire.util.TestsToRun.size()I
[ERROR] at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:123)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.lang.reflect.Method.invoke(Method.java:601)
[ERROR] at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208)
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:158)
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95)
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

The pom.xml looks like this

<dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.31.0</version>
            <scope>compile</scope>
        </dependency>

        <!--If this is commented out parallel testing is not working!!! When in testexecution itself is not working  -->
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.14</version>
        </dependency>

        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-maven</artifactId>
            <version>0.7-groovy-2.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.gmaven</groupId>
                <artifactId>gmaven-plugin</artifactId>
                <version>1.3</version>
                <configuration>
                    <providerSelection>1.7</providerSelection>
                    <source/>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.codehaus.gmaven.runtime</groupId>
                        <artifactId>gmaven-runtime-1.7</artifactId>
                        <version>1.3</version>
                        <exclusions>
                            <exclusion>
                                <groupId>org.codehaus.groovy</groupId>
                                <artifactId>groovy-all</artifactId>
                            </exclusion>
                        </exclusions>
                    </dependency>
                    <dependency>
                        <groupId>org.codehaus.groovy</groupId>
                        <artifactId>groovy-all</artifactId>
                        <version>2.0.6</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.14</version>
                <dependencies>
                </dependencies>
                <configuration>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                    <!-- Surefire 2.14+ configuration for parallelization -->
                    <forkCount>5</forkCount>
                    <reuseForks>false</reuseForks>
                </configuration>
            </plugin>
        </plugins>
    </build>
Swadeshi answered 20/3, 2013 at 14:34 Comment(6)
What happens if you launch your tests sequentially? Use another version of junit, surefire?Veasey
Not much depending on Surefire Version I get for example with 2.12.2 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.2:test (default-test) on project spock-webdriver: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.12.2:test failed: The forked VM terminated without saying properly goodbye. VM crash or System.exit called ? -> [Help 1]Swadeshi
Immediately after adding the spock dependency it crashes for me. I tried it with selenium and junit dependency and it builds. After adding spock dependency to my pom it does not work. Tried several spock versions as wellSwadeshi
Might be an incompatibility between Surefire 2.14 and spock-maven. Try without spock-maven, and make sure that your spec classes conform to Surefire's naming conventions (or reconfigure the naming conventions).Gimlet
Surefire was the problem. I`m using JUnit 4.11 and surefire 2.11 and it works now like a charm.Swadeshi
You should post (and accept) this as an answer, so that your question doesn't remain unanswered.Gimlet
M
6

This is an old thread but for others who might run into this problem ... This error is caused because of an inconsistency with the surefire plugin dependencies and spock 0.7.0 dependencies. Spock uses surefire-junit47, version 2.11. This class/method, JUnit4RunListener.rethrowAnyTestMechanismFailures does not exist in that surefire version. The solution is to exclude the surefire version from the spock dependency definition like this:

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-maven</artifactId>
    <version>0.7-groovy-2.0</version>
    <exclusions>
        <exclusion>  <!-- exclude so we can pick up surefire-junit4 2.17 version for failsafe integration tests  -->
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit47</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Now, it will use the surefire version you've defined, for completeness, here's the surefire dependency I use:

<dependency>
    <groupId>org.apache.maven.surefire</groupId>
    <artifactId>surefire-junit4</artifactId>
    <version>2.17</version>
</dependency>

And then the surefire plugin definition which allows unit and integration tests to fire separately (this part is more about getting unit tests, integration tests, spock, maven and groovy to play nice).

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.17</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit4</artifactId>
            <version>2.17</version>
        </dependency>
    </dependencies>
    <configuration>
        <forkCount>0</forkCount>
        <includes>
            <include>%regex[.*Test.*]</include>
        </includes>
        <excludes>
            <exclude>%regex[.*IntegrationTest.*]</exclude>
        </excludes>
    </configuration>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.17</version>
    <configuration>
        <includes>
            <include>%regex[.*IntegrationTest.*]</include>
        </includes>
    </configuration>
    <executions>
        <execution>
            <id>failsafe-integration-tests</id>
            <goals>
                <goal>integration-test</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Now, unit tests will fire with the test goal and integration tests can be launched independently on demand like this: mvn failsafe:integration-test

Mislead answered 22/7, 2014 at 0:8 Comment(0)
H
2

Try to use version 2.9 or above

<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
Horned answered 11/7, 2013 at 7:17 Comment(0)
S
0

I'm not sure if this is related, but I was running into a "There was an error in the forked process" error when I was running my Grails Spock test in maven. It turned out that I was missing a 's' in my file name:

MyCoolTest.groovy => MyCoolTests.groovy

Subtraction answered 13/5, 2014 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.