使用Junit的Spring Test会话范围bean

时间:2011-02-27 23:51:45

标签: spring spring-mvc junit spring-test

我有一个会话范围的bean,它保存每个http会话的用户数据。我想编写一个Junit测试用例来测试会话范围的bean。我想编写测试用例,以便它可以证明每个会话都会创建bean。 任何指针如何编写这样的Junit测试用例?

3 个答案:

答案 0 :(得分:30)

要在单元测试中使用请求和会话范围,您需要:

  • 在应用程序上下文中注册这些范围
  • 创建模拟会话并请求
  • 通过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();

答案 1 :(得分:30)

我遇到了这种更简单的方法,我想在不论其他人需要它的情况下我也可以发布。

<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/

答案 2 :(得分:10)

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));
    }
}

参考文献: