title: Accessing spring context in testng's @BeforeTest
body: <p>I would like to register some web scopes to the spring context in my <code>@BeforeTest</code> method. But it turns out that spring context is still <code>null</code> at that point.</p>
  
  <p>The test runs fine though, if I change into <code>@BeforeMethod</code>. I wonder how I can access the context in <code>@BeforeTest</code>, because I don't want the scope registration code to be repeated for each test methods.</p>
  
  <p>Below are my code snippets.</p>
  
  <p></p>
  
  <pre><code>public class MyTest extends MyBaseTest {
      @Test public void someTest() { /*...*/ }
  }
  
  @ContextConfiguration(locations="/my-context.xml")
  public class MyBaseTest extends AbstractTestNGSpringContextTests {
      @BeforeTest public void registerWebScopes() {
          ConfigurableBeanFactory factory = (ConfigurableBeanFactory)
                  this.applicationContext.getAutowireCapableBeanFactory();
          factory.registerScope("session", new SessionScope());
          factory.registerScope("request", new RequestScope());
      }   
  
      /* some protected methods here */
  }
  </code></pre>
  
  <p>Here's the error message when running the test:</p>
  
  <pre>
  FAILED CONFIGURATION: @BeforeTest registerWebScopes
  java.lang.NullPointerException
      at my.MyBaseTest.registerWebScopes(MyBaseTest.java:22)
  </pre>