1 package roger.testng; 2 3 import java.lang.reflect.Method; 4 5 import org.testng.annotations.DataProvider; 6 import org.testng.annotations.Test; 7 8 /* 9 * Method 参数 Demo10 * 如果数据提供者的第一个参数是 java.lang.reflect.Method,11 * testng 传递这个将调用的测试方法12 * 如:调用的方法名为 two, 数据提供者就返回213 */14 public class TestDataProviderMethod {15 // Method 表示当前测试的方法16 @DataProvider17 public Object[][] provideNumbers(Method method) {18 Object[][] result = null;19 // 根据方法名称判断20 if (method.getName().equals("one")) {21 result = new Object[][] { new Object[] { 1 } };22 } else if (method.getName().equals("two")) {23 result = new Object[][] { new Object[] { 2 } };24 } else {25 result = new Object[][] { new Object[] { 3 } };26 }27 return result;28 }29 30 @Test(dataProvider = "provideNumbers")31 public void one(int param) {32 System.out.println("one received: " + param);33 }34 35 @Test(dataProvider = "provideNumbers")36 public void two(int param) {37 System.out.println("Two received: " + param);38 }39 40 @Test(dataProvider = "provideNumbers")41 public void three(int param) {42 System.out.println("Three received: " + param);43 }44 45 }