使用Junit的Spring测试会话范围Bean

mbzjlibv  于 2022-11-11  发布在  Spring
关注(0)|答案(3)|浏览(148)

我有一个会话作用域bean,它保存每个http会话的用户数据。我想写一个Junit测试用例来测试会话作用域bean。我想写一个测试用例,这样它就可以证明每个会话都创建了bean。有没有关于如何写这样的Junit测试用例的提示?

blmhpbnm

blmhpbnm1#

若要在公寓测试中使用要求和工作阶段范围,您需要:

  • 在应用程序上下文中注册这些作用域
  • 创建模拟会话和请求
  • 通过RequestContextHolder寄存器模拟请求

类似于以下内容(假设您使用Spring TestContext来运行测试):abstractSessionTest.xml

<beans ...>
    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="session">
                    <bean class="org.springframework.web.context.request.SessionScope" />
                </entry>
                <entry key="request">
                    <bean class="org.springframework.web.context.request.RequestScope" />
                </entry>
            </map>
        </property>
    </bean>
</beans>

@ContextConfiguration("abstractSessionTest.xml")
public abstract class AbstractSessionTest {
    protected MockHttpSession session;
    protected MockHttpServletRequest request;

    protected void startSession() {
        session = new MockHttpSession();
    }

    protected void endSession() {
        session.clearAttributes();
        session = null;
    }

    protected void startRequest() {
        request = new MockHttpServletRequest();
        request.setSession(session);
        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    }

    protected void endRequest() {
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).requestCompleted();
        RequestContextHolder.resetRequestAttributes();
        request = null;
    }
}

现在,您可以在测试代码中使用这些方法:

startSession();
startRequest();
// inside request
endRequest();
startRequest();
// inside another request of the same session
endRequest();
endSession();
doinxwow

doinxwow2#

我遇到了这个更简单的方法,我想我不妨张贴在这里,以防其他人需要它。

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
    <property name="scopes">
        <map>
            <entry key="session">
                <bean class="org.springframework.context.support.SimpleThreadScope"/>
            </entry>
        </map>
    </property>
</bean>

使用这种方法,您不必模拟任何请求/会话对象。
来源: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配置类中的某个位置:

/**
 * 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;
}
ruarlubt

ruarlubt3#

Spring 3.2及更高版本为集成测试提供了对会话/请求作用域Bean的支持

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
@WebAppConfiguration
public class SampleTest {

    @Autowired WebApplicationContext wac;

    @Autowired MockHttpServletRequest request;

    @Autowired MockHttpSession session;    

    @Autowired MySessionBean mySessionBean;

    @Autowired MyRequestBean myRequestBean;

    @Test
    public void requestScope() throws Exception {
        assertThat(myRequestBean)
           .isSameAs(request.getAttribute("myRequestBean"));
        assertThat(myRequestBean)
           .isSameAs(wac.getBean("myRequestBean", MyRequestBean.class));
    }

    @Test
    public void sessionScope() throws Exception {
        assertThat(mySessionBean)
           .isSameAs(session.getAttribute("mySessionBean"));
        assertThat(mySessionBean)
           .isSameAs(wac.getBean("mySessionBean", MySessionBean.class));
    }
}

参考文献:

相关问题