How to run Cucumber Junit tests parallely without sharing data between invoked threads

374 Views Asked by At

I'm running cucumber tests parallelly using below maven configuration:

                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>3.0.0-M5</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <includes>
                                <include>TestRunner.java</include>
                            </includes>
                            <testFailureIgnore>true</testFailureIgnore>
                            <parallel>methods</parallel>
                            <threadCount>${parallelCount}</threadCount>
                            <forkCount>${parallelCount}</forkCount>
                            <reuseForks>false</reuseForks>
                            <perCoreThreadCount>false</perCoreThreadCount>
                        </configuration>
                    </plugin>
                </plugins>

Versions:

<serenity.version>3.2.0</serenity.version>
<cucumber.version>7.2.3</cucumber.version>
<junit.version>4.13.2</junit.version>

Now issue is code is running fine, tests are running parallely but static variables are shared among threads even after using reuseForks = False

Tried various combinations for failsafe config parallel, perCoreThreadCount, useUnlimitedThreads, reuseForks but no luck.

Any idea what changes need to be done to make so that static data is not shared between threads. Thanks!

1

There are 1 best solutions below

0
M.P. Korstanje On

Any idea what changes need to be done to make so that static data is not shared between threads. Thanks!

Fundamentally, it is a property of static fields that there is only one. This means that you can not have a static fields that is not shared by all threads.

Instead you may want to look at using Dependency Injection. This will allow you avoid the use of static fields by injecting data into your step definition files. This data will be scoped to a scenario and not leak out (unless you use static fields ofcourse).