栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

具有会话支持的Spring MVC 3.1集成测试

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

具有会话支持的Spring MVC 3.1集成测试

我已经以某种round回的方式完成了-尽管可以。我要做的是让Spring-
Security创建一个会话,并在会话中填充相关的Security属性,然后以这种方式获取该会话:

    this.mockMvc.perform(post("/j_spring_security_check") .param("j_username", "fred") .param("j_password", "fredspassword")) .andExpect(status().isMovedTemporarily()) .andDo(new ResultHandler() {     @Override     public void handle(MvcResult result) throws Exception {         sessionHolder.setSession(new SessionWrapper(result.getRequest().getSession()));     } });

SessionHolder是我的自定义类,仅用于保存会话:

private static final class SessionHolder{    private SessionWrapper session;    public SessionWrapper getSession() {        return session;    }    public void setSession(SessionWrapper session) {        this.session = session;    }}

而SessionWrapper是从MockHttpSession扩展的另一个类,仅因为会话方法需要MockHttpSession:

private static class SessionWrapper extends MockHttpSession{    private final HttpSession httpSession;    public SessionWrapper(HttpSession httpSession){        this.httpSession = httpSession;    }    @Override    public Object getAttribute(String name) {        return this.httpSession.getAttribute(name);    }}

有了这些设置,现在您可以简单地从sessionHolder中获取会话并执行后续方法,例如。就我而言:

mockMvc.perform(get("/membersjson/1").contentType(MediaType.APPLICATION_JSON).session(sessionHolder.getSession())) .andExpect(status().isOk()) .andExpect(content().string(containsString("OneUpdated")));


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/637275.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号