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:

  1. Create methods for saving/retrieving data with "java.util.concurrent.Locks"
  2. 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");
            return new ArrayList<>();
        return textFormatter.getListfromStringDelimiterNonTrimmed(listStr, ",");
    public static void logUnitReturned(String unitName) {
        String listStr = getProperty("UNITS_RETURNED");
            setProperty("UNITS_RETURNED", unitName);
            setProperty("UNITS_RETURNED", getProperty("UNITS_RETURNED")+","+unitName);
  • How do you run your tests in parallel?
    – Alexey R.
    Aug 5 '20 at 10:09
  • like this:<!DOCTYPE suite SYSTEM "testng.org/testng-1.0.dtd"> <suite name="APP Tests Suite" verbose="0" parallel="true" preserve-order="true"> Aug 5 '20 at 10:11
  • Can you show your code? Why aren't you just use data providers which provide the objects for each particular test separately?
    – Alexey R.
    Aug 5 '20 at 11:02
  • @AlexeyR. , the data to be used is dependent on available data in the app. For e.g. there's 5 different customers. I want my suite to take each customer then perform different tests. Like for customer 1, will delete ; customer 2, will update ..and so on. Aug 6 '20 at 3:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.