1 package test.dataprovider; 2 3 import org.testng.Assert; 4 import org.testng.annotations.DataProvider; 5 import org.testng.annotations.Test; 6 7 import java.lang.reflect.Method; 8 9 public class MethodTest { 10 11 @DataProvider(name = "dp1") createData(Method m)12 public Object[][] createData(Method m) { 13 Assert.assertEquals("test1", m.getName()); 14 Assert.assertEquals("test.dataprovider.MethodTest", m.getDeclaringClass().getName()); 15 return new Object[][] { 16 new Object[] { "Cedric" }, 17 new Object[] { "Alois" }, 18 }; 19 } 20 21 @Test(dataProvider = "dp1") test1(String s)22 public void test1(String s) { 23 Assert.assertTrue("Cedric".equals(s) || "Alois".equals(s)); 24 } 25 26 private int m_test2 = 0; 27 private int m_test3 = 0; 28 29 @DataProvider(name = "dp2") createData2(Method m)30 public Object[][] createData2(Method m) { 31 if ("test2".equals(m.getName())) { 32 m_test2++; 33 } else if ("test3".equals(m.getName())) { 34 m_test3++; 35 } else { 36 throw new RuntimeException("Received method " + m + ", expected test2 or test3"); 37 } 38 Assert.assertEquals("test.dataprovider.MethodTest", m.getDeclaringClass().getName()); 39 return new Object[][] { 40 new Object[] { "Cedric" }, 41 }; 42 } 43 44 @Test(dataProvider = "dp2") test2(String s)45 public void test2(String s) { 46 Assert.assertTrue("Cedric".equals(s)); 47 } 48 49 @Test(dataProvider = "dp2") test3(String s)50 public void test3(String s) { 51 Assert.assertTrue("Cedric".equals(s)); 52 } 53 54 @Test(dependsOnMethods = {"test2", "test3"}) multipleTestMethods()55 public void multipleTestMethods() { 56 Assert.assertEquals(1, m_test2); 57 Assert.assertEquals(1, m_test3); 58 } 59 60 } 61