使用Junit的Spring Test会话范围bean

Sha*_*mik 32 junit spring spring-mvc spring-test

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

axt*_*avt 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>
Run Code Online (Sandbox Code Playgroud)

.

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

startSession();
startRequest();
// inside request
endRequest();
startRequest();
// inside another request of the same session
endRequest();
endSession();
Run Code Online (Sandbox Code Playgroud)


kct*_*ang 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>
Run Code Online (Sandbox Code Playgroud)

使用此方法,您不必模拟任何请求/会话对象.

资料来源:http://tarunsapra.wordpress.com/2011/06/28/junit-spring-session-and-request-scope-beans/


Mar*_*szS 11

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));
    }
}
Run Code Online (Sandbox Code Playgroud)

参考文献:

  • 添加WebAppConfiguration后,我遇到了一个initializationError错误,我解决了将servlet-api添加到pom文件中的问题,如下所述:http://nickebbitt.wordpress.com/2013/12/20/spring-3-2-mvc-unit-测试-initializationerror-使用-webappconfiguration / (2认同)