As of Spring 3.2, you can have request-scoped and session-scoped beans in your tests, that can be read in the Spring reference manual, section 11.3.5.
For example:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration({"classpath:applicationContext.xml"})
public class Test1
{
@Autowired
private MySessionBean state;
@Test
public void test() {
System.out.println(state.toString());
}
}
The above works. However, trying to adjust this to TestNG:
@WebAppConfiguration
@ContextConfiguration({"classpath:applicationContext.xml"})
public class Test2 extends AbstractTestNGSpringContextTests
{
@Autowired
private MySessionBean state;
@Test
public void test() {
System.out.println(state.toString());
}
}
This will throw an exception:
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread?
Am I doing something wrong or is testing with session-scoped beans only supported with JUnit and not TestNG?
If you're not on Spring Framework 3.2.7 or later, this won't work by default.
See SPR-11340 for details.
As a work-around, you can declare your test class as follows
Regards,
Sam