1package test.sample;
2
3import org.testng.annotations.AfterClass;
4import org.testng.annotations.AfterSuite;
5import org.testng.annotations.Test;
6
7
8/**
9 * This class is used to test invocationCountTest
10 *
11 * @author cbeust
12 */
13public class InvocationCountTest {
14
15  //
16  // Invocation test
17  //
18  private static int m_count = 0;
19
20  @AfterSuite(groups = {"invocationOnly"})
21  public void afterSuite() {
22    m_count = 0;
23    m_count2 = 0;
24    m_count3 = 0;
25  }
26
27  @Test(groups = { "invocationOnly"}, invocationCount = 10 )
28  public void tenTimesShouldSucceed() {
29    m_count++;
30  }
31
32  //
33  // Invocation + Success percentage test
34  // This method will work the first 8 times and fail after that, but overall
35  // the test should still pass because successPercentage = 80
36  //
37  private static int m_count2 = 0;
38
39  @Test(groups = { "successPercentageThatSucceedsOnly" },
40    invocationCount = 10, successPercentage = 80)
41  public void successPercentageShouldSucceed() {
42    if (m_count2 >= 8) {
43      throw new RuntimeException("Called more than eight times : " + m_count2);
44    }
45    m_count2++;
46  }
47
48  //
49  // Invocation + Success percentage test
50  // This method will work the first 8 times and fail after that.  One of
51  // the failures will fall under the percentage tolerance but the next one
52  // will not.
53  //
54  private static int m_count3 = 0;
55
56  @Test(groups = { "successPercentageThatFailsOnly" },
57    invocationCount = 10, successPercentage = 90)
58  public void successPercentageShouldFail() {
59    if (m_count3>= 8) {
60      throw new RuntimeException("Called more than eight times : " + m_count3);
61    }
62    m_count3++;
63  }
64
65  @AfterClass(groups = { "invocationOnly"})
66  public void verify() {
67    assert 10 == m_count : "Method should have been invoked 10 times but was invoked "
68      + m_count + " times";
69  }
70
71  public static void ppp(String s) {
72    System.out.println("[InvocationCount] " + s);
73  }
74
75}
76