Home | History | Annotate | Download | only in invocationcount
      1 package test.invocationcount;
      2 
      3 import org.testng.TestNG;
      4 import org.testng.annotations.Test;
      5 
      6 import java.util.List;
      7 
      8 import test.InvokedMethodNameListener;
      9 import test.SimpleBaseTest;
     10 
     11 import static org.assertj.core.api.Assertions.assertThat;
     12 
     13 /**
     14  * Test various combination of @BeforeMethod(firstTimeOnly = true/false) and
     15  * @AfterMethod(lastTimeOnly = true/false) with invocation counts and data
     16  * providers.
     17  * @author cbeust (at) google.com
     18  *
     19  */
     20 public class FirstAndLastTimeTest extends SimpleBaseTest {
     21   @Test
     22   public void verifyDataProviderFalseFalse() {
     23     List<String> invokedMethodNames = run(DataProviderFalseFalseTest.class);
     24 
     25     assertThat(invokedMethodNames).containsExactly(
     26         "beforeMethod", "f", "afterMethod",
     27         "beforeMethod", "f", "afterMethod",
     28         "beforeMethod", "f", "afterMethod"
     29     );
     30   }
     31 
     32   @Test
     33   public void verifyDataProviderTrueFalse() {
     34     List<String> invokedMethodNames = run(DataProviderTrueFalseTest.class);
     35 
     36     assertThat(invokedMethodNames).containsExactly(
     37         "beforeMethod", "f", "afterMethod",
     38         "f", "afterMethod",
     39         "f", "afterMethod"
     40     );
     41   }
     42 
     43   @Test
     44   public void verifyDataProviderFalseTrue() {
     45     List<String> invokedMethodNames = run(DataProviderFalseTrueTest.class);
     46 
     47     assertThat(invokedMethodNames).containsExactly(
     48         "beforeMethod", "f",
     49         "beforeMethod", "f",
     50         "beforeMethod", "f", "afterMethod"
     51     );
     52   }
     53 
     54   @Test
     55   public void verifyDataProviderTrueTrue() {
     56     List<String> invokedMethodNames = run(DataProviderTrueTrueTest.class);
     57 
     58     assertThat(invokedMethodNames).containsExactly(
     59         "beforeMethod", "f",
     60         "f",
     61         "f", "afterMethod"
     62     );
     63   }
     64 
     65   @Test
     66   public void verifyInvocationCountFalseFalse() {
     67     List<String> invokedMethodNames = run(InvocationCountFalseFalseTest.class);
     68 
     69     assertThat(invokedMethodNames).containsExactly(
     70         "beforeMethod", "f", "afterMethod",
     71         "beforeMethod", "f", "afterMethod",
     72         "beforeMethod", "f", "afterMethod"
     73     );
     74   }
     75 
     76   @Test
     77   public void verifyInvocationCountTrueFalse() {
     78     List<String> invokedMethodNames = run(InvocationCountTrueFalseTest.class);
     79 
     80     assertThat(invokedMethodNames).containsExactly(
     81         "beforeMethod", "f", "afterMethod",
     82         "f", "afterMethod",
     83         "f", "afterMethod"
     84     );
     85   }
     86 
     87   @Test
     88   public void verifyInvocationCountFalseTrue() {
     89     List<String> invokedMethodNames = run(InvocationCountFalseTrueTest.class);
     90 
     91     assertThat(invokedMethodNames).containsExactly(
     92         "beforeMethod", "f",
     93         "beforeMethod", "f",
     94         "beforeMethod", "f", "afterMethod"
     95     );
     96   }
     97 
     98   @Test
     99   public void verifyInvocationCountTrueTrue() {
    100     List<String> invokedMethodNames = run(InvocationCountTrueTrueTest.class);
    101 
    102     assertThat(invokedMethodNames).containsExactly(
    103         "beforeMethod", "f",
    104         "f",
    105         "f", "afterMethod"
    106     );
    107   }
    108 
    109   private static List<String> run(Class<?> cls) {
    110     TestNG tng = create(cls);
    111     InvokedMethodNameListener listener = new InvokedMethodNameListener();
    112     tng.addListener(listener);
    113 
    114     tng.run();
    115 
    116     return listener.getInvokedMethodNames();
    117   }
    118 
    119 }
    120