【发布时间】:2014-03-18 18:23:37
【问题描述】:
从 Spring 3.2 开始,您可以在测试中使用请求范围和会话范围的 bean,可以读取 in the Spring reference manual, section 11.3.5。
例如:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:applicationContext.xml"})
public class Test1
{
@Autowired
private MySessionBean state;
@Test
public void test() {
System.out.println(state.toString());
}
}
以上工作。但是,尝试将其调整为 TestNG:
@WebAppConfiguration
@ContextConfiguration({"classpath:applicationContext.xml"})
public class Test2 extends AbstractTestNGSpringContextTests
{
@Autowired
private MySessionBean state;
@Test
public void test() {
System.out.println(state.toString());
}
}
这会抛出异常:
java.lang.IllegalStateException:未找到线程绑定请求:是 您指的是实际 Web 请求之外的请求属性, 还是在原始接收线程之外处理请求?
我是在做错什么,还是使用仅受 JUnit 支持而不支持 TestNG 的会话范围 bean 进行测试?
【问题讨论】:
标签: spring testng spring-test