How To Use ThreadSafe Singleton Class To Manage Instance Variables In Automation Framework – Java


In the previous post, we have seen how maximum beginners and experienced Selenium professionals apply inheritance with TestNG annotations while setup test scripts and end up with encountering NullPointerException. We have solved NullPointerException by converting instance variables to Static variables.

We also solved the above problem using a Singleton design pattern as well. I have used a static reference of class SharedVariables which will create a problem while running in parallel for sure. So in this post, we will use ThreadLocal static instance of class SharedVariables.

Dependency version details

I used below version of TestNG in this post:-

Problem with static variables

Before we jump to ThreadLocal implementation, Let’s create a scenario in which we will face incorrect mapping of data while running in parallel.

There is no change in class SharedVariable and SharedVariableMap.

Class SharedVariable

Class SharedVariableMap

Add thread details for understanding calling flow well and random wait in Setup class and only thread details in test methods classes. I will also change annotation BeforeSuite to BeforeMethod in Setup class. I will run methods in parallel so there will be a call to setupVariable() for each method by a unique thread and may override other’s values.

Class Setup

Class FirstTestNGClass

Class SecondTestNGClass

Create a TestNG xml just to run methods in parallel.


Lengthy output to understand and I may not be able to explain well. Let’s group console output by thread ids as below.

Thread 11

Thread 12

Thread 13

Thread 14

I have just listed for 4 thread ids. Values initialized by Thread 11 and Thread 14 are consumed by test method called by Thread 11 and Thread 14 respectively. You can see values are matched. This behavior will change in each run.

Now observe carefully for Thread 12 and Thread 13. Thread 12 has no issues but test methods called by Thread 13 have not used values initialized by Thread 13 but by Thread 12. As we are running methods in parallel so each Test method should have their own values but here it is messed. This is a problem while running parallel with static variables.

Solving Problem with static variables during parallel run using ThreadLocal

I have explained ThreadLocal in my previous posts. You can check those in the prerequisite section. In this section, we will just focus on the implementation of ThreadLocal.

Let’s have instance variables as ThreadLocal in both SharedVariable and SharedvariableMap classes.

Class SharedVariables

Class SharedVariablesMap

Class Setup

I have not initialized class variables sharedVariables and sharedVariablesMap as we are running methods in parallel. If we initialized while declaration, these will be initialized commonly for all test methods when class Setup will be loaded into memory and you will see incorrect data mapping among threads again.

There is no change in Test classes. Below is TestNG xml.



Let’s group them by thread ids.

This time you can see there is no wrong mapping of data and each thread was able to access correct values.

You can download/clone the above sample project from here.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Writing technical posts and creating YouTube videos are my hobbies.

1 thought on “How To Use ThreadSafe Singleton Class To Manage Instance Variables In Automation Framework – Java

  1. Hi Amod Nice article, Can we able to use same singleton pattern in Selenium C# to handle threadsafe webdriver?

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.