This question is similar to How to manage application data when running multiple tests in parallel?
But I want to know if there's a way or listener in testNG that will lock certain method when running tests in parallel.
What I tried was:
- Create methods for saving/retrieving data with "java.util.concurrent.Locks"
- Add those methods in "beforeInitialize" listener
However, the test instance happens before "beforeInitialize". In short, there's still possibility that it access those methods(with lock) at the same time, using different instance.
Edit (1) : Added sample code I used to log and retrieve used data
public static List<String> unitsReturned(){
String listStr = getProperty("UNITS_RETURNED");
if(listStr.equals("UNITS_RETURNED"))
return new ArrayList<>();
return textFormatter.getListfromStringDelimiterNonTrimmed(listStr, ",");
}
public static void logUnitReturned(String unitName) {
String listStr = getProperty("UNITS_RETURNED");
if(listStr.equals("UNITS_RETURNED"))
setProperty("UNITS_RETURNED", unitName);
else
setProperty("UNITS_RETURNED", getProperty("UNITS_RETURNED")+","+unitName);
}