I am writing few test in which I am using DataProvider for @Test and creating few thing now as a cleanup/teardown() step I want to delete these things in @After methods, how can I use DataProvider in AfterTest(), AfterClass(), AfterMethod() ?
Asked
Active
Viewed 556 times
1 Answers
0
It is possible. For example TestNg can inject the same objects to @AfterMethod
. See the example below:
@DataProvider(name = "test")
public Object[][] testDataProvide(){
return new Object[][]{
{"11", "12"},
{"21", "22"}
};
}
@Test(dataProvider = "test")
public void testDP(String one, String two){
System.out.println(String.join(",", one, two));
}
@AfterMethod
public void tearDownEach(Object[] args){
System.out.println("Tearing down: " + String.join(",", args[0].toString(), args[1].toString()));
}
P.S. - AfterTest()
and AfterClass()
do not have such way since they run after a bunch of tests have completed which does not make sense to use with data provider that is intended to supply a piece of data to every single test.

Alexey R.
- 8,057
- 2
- 11
- 27
-
I tried `AfterMethod` as you suggested but I am getting this error : `org.testng.TestNGException: Can inject only one of
into a @AfterMethod annotated deleteAndValidate. ` – ssharma Aug 07 '20 at 18:05 -
`@DataProvider(name = "body") public Object[] body() throws IOException{ JsonArray jArray = HelperMethods.ReadJsonFileReturnJsonArray(filePath); Object[] data = new Object[jArray.size()]; for (int i = 0; i
– ssharma Aug 07 '20 at 19:36 -
1 - Data provider has to return `Object[][]`, not just `Object[]`. 2 - AfterMethod has to take `Object[]`, not just `Object`. Please carefully check my example and adopt it to your case. – Alexey R. Aug 07 '20 at 21:09