title: AfterAll global hook cucumber-jvm
body: <p>I'm using <code>cucumber-jvm</code> in my integration tests and I need to execute some code after all scenarios are finished, just once.</p>
  
  <p>After reading carefully some posts like <a href="http://ramblingsofaswtester.com/?p=146" rel="nofollow">this</a> and reviewed this reported <a href="https://github.com/cucumber/cucumber-jvm/issues/515" rel="nofollow">issue</a>, I've accomplished it doing something like this:</p>
  
  <pre><code>public class ContextSteps {
  
     private static boolean initialized = false;
  
     @cucumber.api.java.Before
     public void setUp() throws Exception {
        if (!initialized) {
           // Init context. Run just once before first scenario starts
  
           Runtime.getRuntime().addShutdownHook(new Thread() {
              @Override
              public void run() {
                // End context. Run just once after all scenarios are finished
              }
           });
  
           initialized = true;
        }
     }
  }
  </code></pre>
  
  <p>I think the context initialization (equivalent to <code>BeforeAll</code>) done in this way is fine. However, although it's working, I'm not sure at all if the <code>AfterAll</code> simulation using <code>Runtime.getRuntime().addShutdownHook()</code> is a good practice.</p>
  
  <p>So, these are my questions:</p>
  
  <ul>
  <li>Should I avoid <code>Runtime.getRuntime().addShutdownHook()</code> to implement <code>AfterAll</code>?</li>
  <li>Are there other better choices to emulate the <code>AfterAll</code> behaviour in cucumber-jvm?</li>
  </ul>
  
  <p>Thanks in advance for the help.</p>