TestNG - Instance variable in test class gets overwritten while trying to execute test cases in parallel="methods" mode

21 views Asked by At

I have written below testNG test class.

import org.testng.annotations.*;

@Test
public class Dummy_001 {
    String temp;

    public void test_1(){
         temp = "Sample 1";
        System.out.println(temp);
    }


    public void test_2(){
        temp = "Sample 2";
        System.out.println(temp);
    }

    public void test_3(){
        String temp = "Sample 3";
        System.out.println(temp);
    }
}

I am executing the test with an xml file as below.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Exercise Suite" parallel="methods"  thread-count="3">
    <test name="Lambda Test Exercise 1">       
        <classes>
        <class name="ui.Dummy_001">
        <methods>
            <include name="test_1"/>
            <include name="test_2"/>
            <include name="test_3"/>
        </methods>
        </class>
        </classes>
    </test>
</suite>

I am expecting to have the temp to have method specific values during the parallel execution. In actual execution I am getting an output as follows. Execution Screenshot

Is this an expected behavior. If yes then is there any way to prevent the instance variables gets overwritten by different threads and to have different instance variables to each thread.

0

There are 0 answers