设置多个会话变量

时间:2016-07-11 04:52:12

标签: java spring session junit controller

我已经为弹簧控制器编写了测试用例。我在httpservletrequest中设置了多个会话属性。

WebApplicationContext wac;
MockHttpSession session= new MockHttpSession(wac.getServletContext)
MockHttpServletRequest request= new MockHttpServletRequest (wac.getServletContext)

我将单个会话变量设置为

session.setAttribute("uname","jones");
request.setSession(session);

但是如何添加多个会话对象,如密码,年龄等等

session.setAttribute("pwd","zzzzzz");
session.setAttribute("age","44");

当我这样做时,它会覆盖以前的会话对象。

1 个答案:

答案 0 :(得分:0)

您可以尝试创建多个会话变量,如下所示:

// Create a session object if it is already not  created.
  HttpSession session = request.getSession(true);
// set session attributes
  session.setAttribute("uname","jones");
  session.setAttribute("pwd","zzzzzz");
  session.setAttribute("age","44");