title: TestNG: Identifying which tests methods are next
body: <p>My goal is to clear() my javax.persistence.EntityManager after each test method. </p>
  
  <p>Here's an example of a test class:</p>
  
  <pre><code>public class Example
  {
      @Test(dataProvider = "sampleDataProvider")
      public void testA(String parameter)
      {
          System.out.println(parameter);
      }
  
      @Test(dataProvider = "sampleDataProvider")
      public void testB(String parameter)
      {
          System.out.println(parameter);
      }
  }
  </code></pre>
  
  <p>The entityManager is used in the dataProvider "sampleDataProvider" by querying the DB for test data which is then compiled in this format: <code>new Object[2][1]</code>. Keep in mind that the querying and compiling of data is all done before a test method (annotated with @DataProvider) is actually run and that we're actually querying for entities and not just Strings.</p>
  
  <p>The above test class would run like so:</p>
  
  <pre><code>testA("some queried entity 1")
  testA("some queried entity 2")
  testB("some queried entity 1")
  testB("some queried entity 2")
  </code></pre>
  
  <p>My initial solution was to use the <code>@AfterTest</code> annotation to clear the entityManager. However it would detach <code>"some queried entity 2"</code> from the entityManager before the second-runs (or second test instances) of <code>testA</code> and <code>testB</code> which causes problems on read/write operations to the members of <code>"some queried entity 2"</code>.</p>
  
  <p>My goal is to clear the entityManager <em>after a test method</em>, and not necessarily after every instance of a test method. </p>
  
  <p>Does TestNG make it possible to know which test is run next? That way I could easily clear the entityManager if the next test is a new one.</p>
  
  <p>Any other recommendations?</p>