startSession();
startRequest();
// inside request
endRequest();
startRequest();
// inside another request of the same session
endRequest();
endSession();
/**
* Used in CustomScopeConfigurer bean below
*
* @return
*/
@Bean
public static SimpleThreadScope simpleThreadScope()
{
return new SimpleThreadScope();
}
/**
* This bean is needed in order to mimic spring's SessionScope
*
* @param aSimpleThreadScope
* @return
*/
@Bean
public static CustomScopeConfigurer customScopeConfigurer(SimpleThreadScope aSimpleThreadScope)
{
CustomScopeConfigurer result = new CustomScopeConfigurer();
result.addScope( "session", aSimpleThreadScope );
return result;
}
3条答案
按热度按时间blmhpbnm1#
若要在公寓测试中使用要求和工作阶段范围,您需要:
RequestContextHolder
寄存器模拟请求类似于以下内容(假设您使用Spring TestContext来运行测试):
abstractSessionTest.xml
:。
现在,您可以在测试代码中使用这些方法:
doinxwow2#
我遇到了这个更简单的方法,我想我不妨张贴在这里,以防其他人需要它。
使用这种方法,您不必模拟任何请求/会话对象。
来源:http://tarunsapra.wordpress.com/2011/06/28/junit-spring-session-and-request-scope-beans/
spring 5.x的更新(在spring 5.3.22中工作,在JUnit 5测试中,由@ExtendWith(SpringExtension.class)注解):将以下代码放在Spring配置类中的某个位置:
ruarlubt3#
Spring 3.2及更高版本为集成测试提供了对会话/请求作用域Bean的支持
参考文献: