When porting some tests from JUnit to TestNG, I ran into a problem due to differences in the way these test environments handle instances of the test class.
JUnit creates a new instance of the Test class for each test method. So, the general template that I see is:
public class MyTest { private Stream inputData; @Before public void setUp() {
In contrast, TestNG uses one instance of the Test class for all test methods. So the above pattern does not work! Because data is stored in instance fields, values ββare no longer isolated. This can lead to overwriting of intermediate data if parallel execution is enabled.
So how would I do this with TestNG? Is there a way to store data that is isolated for each set @BeforeMethod - @Test - @AfterMethod ?
I can do all 3 steps inside @Test itself, but for this I will need to add disappointing try...finally blocks to each test. I also tried using ITestContext , but it also seems to be common to the entire test run.
source share