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