Below factory class is used to get the drivers for execution
public class DriverFactory {
//holds the device config
public static Map<String, String> devConfig = new ConcurrentHashMap<>();
//other lines of code follow
}
This config is loaded in a junit class from an external data source as below:
@RunWith(ConcurrentTestRunner.class)
public class MyTestRunner{
static final int THREAD_COUNT = 1;
@ThreadCount(THREAD_COUNT) @Override @Test
public void run(){
// Devices class returns config for the device
DriverFactory.devConfig = Devices.getConfig()
//other lines of code to perform execution
}
}
If device config is required in other class during execution, it is accessed like below:
public class MobileActions{
public void swipe(){
String pf = DriverFactory.devConfig.get("Platform");
//other lines of code
}
}
This approach of (having devConfig as static) works fine there is one thread. Now, to support parallel execution across device, if thread count is changed to 2, devConfig will always have the value set by 2nd thread.
In order to avoid this problem, if devConfig is made an non-static, we have to inject this variable in all other classes for e.g., in the above MobileActions class. Is there a way that this variable can remain static but still work during multithreaded execution(each thread should deal with it's own copy of devConfig). We also tried making this variable as ThreadLocal<>, but that didn't help either.
Any help is much appreciated! Thanks