This should solve your use case (Remember to use TestNG v7.9.0 which is the latest release if you are on JDK11 and TestNG v7.5.1 if you are on JDK8)
import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import static java.lang.annotation.ElementType.PARAMETER;
public class Sample {
/**
* A marker annotation which can be used to annotate the method parameters of a data driven test
* to indicate that a particular parameter is to be regarded as a reference number which will be used
* for special processing.
*/
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({PARAMETER})
public @interface Reference {}
@BeforeMethod
public void beforeMethod(ITestResult itr) {
Method testMethodThatIsAboutToBeExecuted = itr.getMethod().getConstructorOrMethod().getMethod();
int position = -1;
for (Parameter each : testMethodThatIsAboutToBeExecuted.getParameters()) {
position += 1;
if (each.isAnnotationPresent(Reference.class)) {
break;
}
}
if (position != -1) {
Object reference = itr.getParameters()[position];
System.err.println("Reference : " + reference);
}
}
@Test(dataProvider = "getData")
public void testMethod(int number, @Reference String text) {
System.err.printf("Test method received the parameters ( %d, %s) \n", number, text);
}
@DataProvider
public Object[][] getData() {
return new Object[][]{
{1, "one"},
{2, "two"}
};
}
}
Thanks & Regards
Krishnan Mahadevan
"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/