【发布时间】:2012-04-28 09:37:58
【问题描述】:
我想在我的 @BeforeTest 方法中将一些 web 范围注册到 spring 上下文中。但事实证明,此时 spring 上下文仍然是null。
如果我更改为@BeforeMethod,测试运行良好。我想知道如何访问@BeforeTest 中的上下文,因为我不希望每个测试方法都重复范围注册代码。
下面是我的代码 sn-ps。
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)【问题讨论】:
标签: java testng spring-test