私のメソッドでいくつかの Web スコープをスプリング コンテキストに登録したいと思います@BeforeTest。しかし、春のコンテキストはまだnullその時点にあることがわかりました。
ただし、に変更すると、テストは正常に実行され@BeforeMethodます。@BeforeTestテスト メソッドごとにスコープ登録コードを繰り返したくないので、 のコンテキストにどのようにアクセスできるのだろうか。
以下は私のコードスニペットです。
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 */
}
テスト実行時のエラーメッセージは次のとおりです。
失敗した構成: @BeforeTest registerWebScopes
java.lang.NullPointerException
my.MyBaseTest.registerWebScopes (MyBaseTest.java:22) で