Bhuvan Bhuvan - 1 month ago 7x
Java Question

How to configure multi-threading in TestNG having @DataProvider, @Factory and one @Test method

I want to have multi-threading in my class, i.e., my

method should run in parallel.

The test class has one
annotated method, one
and one
method which is a constructor of my test-class.

I have also overridden
getTestName(ITest class)
method. My test-data has 28 scenarios.


@Listeners com.loyalty.HTMLReporter.class
public class TestClass implemented ITest {

public static Object[][] dp () {
//I read test data from a file. The test-data could vary from 2 to 1000
Object tests [][] = new Object [testcaseIdMap.keyset().toArray().length][];

for (Object ket : testcaseIdMap.keySet().toArray()){
tests[i] = new Object[1];
tests[i][0] = key.toString();
return tests

@Factory (dataProvider = "dp")
public TestClass (String testcaseId) {
this.testcaseId = testcaseId;

public void TC () {
//some code

public String getTestName () {
return testcaseId;

public void createDBConnection () {
// some code

public void failedTestCases () {
//some code


If you want to run your test methods parallel, create a test suite xml file like below

<suite name="Your suite name" parallel="methods" thread-count="2" >
    <test name="Your test name">
            <class name="packagename.classname" />

Set number of threads as per your need.

If you want to run your dataprovider parallel, then add parallel=true in the dataprovider annotation.

@DataProvider(name = "dataprovidername",parallel=true)