单元测试怎样设值到session 中

单元测试怎样设值到session 中
这个问题纠结了会,找了个高手,终于解决了.
思路:构造context对象,并且构造request对象和session对象

TestRequest 就是构造的request 里面有TestSession

ExtHttpDoradoContext 继承HttpDoradoContext

A.注册context
TestRequest.registerDoradoContext();
A.1设置上下文环境
DoradoContext.registerContext(new ExtHttpDoradoContext());
A.1.1构造ExtHttpDoradoContext的时候,将TestRequest 实例设置给
context中的request对象

所以在工具类中能够娶到数据
HttpDoradoContext context = (HttpDoradoContext) DoradoContext.getContext();
return context.getRequest().getSession().getId();

/***============TestRequest 开始==================****/
package com.bstek.scm;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Principal;
import java.util.Enumeration;
import java.util.Locale;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionContext;

import com.bstek.dorado.common.DoradoContext;
import com.bstek.dorado.common.ExtHttpDoradoContext;


public class TestRequest implements HttpServletRequest {

public static void registerDoradoContext(){
DoradoContext.registerContext(new ExtHttpDoradoContext());
}

public static void unregisterDoradoContext() {
DoradoContext.unregisterContext();
}

protected class TestSession implements HttpSession {

@Override
public Object getAttribute(String name) {
// TODO Auto-generated method stub
return null;
}

@Override
public Enumeration getAttributeNames() {
// TODO Auto-generated method stub
return null;
}

@Override
public long getCreationTime() {
// TODO Auto-generated method stub
return 0;
}

@Override
public String getId() {
return "test";
}

@Override
public long getLastAccessedTime() {
// TODO Auto-generated method stub
return 0;
}

@Override
public int getMaxInactiveInterval() {
// TODO Auto-generated method stub
return 0;
}

@Override
public ServletContext getServletContext() {
// TODO Auto-generated method stub
return null;
}

@Override
public HttpSessionContext getSessionContext() {
// TODO Auto-generated method stub
return null;
}

@Override
public Object getValue(String name) {
// TODO Auto-generated method stub
return null;
}

@Override
public String[] getValueNames() {
// TODO Auto-generated method stub
return null;
}

@Override
public void invalidate() {
// TODO Auto-generated method stub

}

@Override
public boolean isNew() {
// TODO Auto-generated method stub
return false;
}

@Override
public void putValue(String name, Object value) {
// TODO Auto-generated method stub

}

@Override
public void removeAttribute(String name) {
// TODO Auto-generated method stub

}

@Override
public void removeValue(String name) {
// TODO Auto-generated method stub

}

@Override
public void setAttribute(String name, Object value) {
// TODO Auto-generated method stub

}

@Override
public void setMaxInactiveInterval(int interval) {
// TODO Auto-generated method stub

}
}

private final TestSession session;

public TestRequest(){
session = new TestSession();
}

@Override
public String getAuthType() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getContextPath() {
// TODO Auto-generated method stub
return null;
}

@Override
public Cookie[] getCookies() {
// TODO Auto-generated method stub
return null;
}

@Override
public long getDateHeader(String arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public String getHeader(String arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public Enumeration getHeaderNames() {
// TODO Auto-generated method stub
return null;
}

@Override
public Enumeration getHeaders(String arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public int getIntHeader(String arg0) {
// TODO Auto-generated method stub
return 0;
}

@Override
public String getMethod() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getPathInfo() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getPathTranslated() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getQueryString() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getRemoteUser() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getRequestURI() {
// TODO Auto-generated method stub
return null;
}

@Override
public StringBuffer getRequestURL() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getRequestedSessionId() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getServletPath() {
// TODO Auto-generated method stub
return null;
}

@Override
public HttpSession getSession() {
return session;
}

@Override
public HttpSession getSession(boolean arg0) {
return session;
}

@Override
public Principal getUserPrincipal() {
// TODO Auto-generated method stub
return null;
}

@Override
public boolean isRequestedSessionIdFromCookie() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isRequestedSessionIdFromURL() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isRequestedSessionIdFromUrl() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isRequestedSessionIdValid() {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean isUserInRole(String arg0) {
// TODO Auto-generated method stub
return false;
}

@Override
public Object getAttribute(String arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public Enumeration getAttributeNames() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getCharacterEncoding() {
// TODO Auto-generated method stub
return null;
}

@Override
public int getContentLength() {
// TODO Auto-generated method stub
return 0;
}

@Override
public String getContentType() {
// TODO Auto-generated method stub
return null;
}

@Override
public ServletInputStream getInputStream() throws IOException {
// TODO Auto-generated method stub
return null;
}

@Override
public String getLocalAddr() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getLocalName() {
// TODO Auto-generated method stub
return null;
}

@Override
public int getLocalPort() {
// TODO Auto-generated method stub
return 0;
}

@Override
public Locale getLocale() {
// TODO Auto-generated method stub
return null;
}

@Override
public Enumeration getLocales() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getParameter(String arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public Map getParameterMap() {
// TODO Auto-generated method stub
return null;
}

@Override
public Enumeration getParameterNames() {
// TODO Auto-generated method stub
return null;
}

@Override
public String[] getParameterValues(String arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public String getProtocol() {
// TODO Auto-generated method stub
return null;
}

@Override
public BufferedReader getReader() throws IOException {
// TODO Auto-generated method stub
return null;
}

@Override
public String getRealPath(String arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public String getRemoteAddr() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getRemoteHost() {
// TODO Auto-generated method stub
return null;
}

@Override
public int getRemotePort() {
// TODO Auto-generated method stub
return 0;
}

@Override
public RequestDispatcher getRequestDispatcher(String arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public String getScheme() {
// TODO Auto-generated method stub
return null;
}

@Override
public String getServerName() {
// TODO Auto-generated method stub
return null;
}

@Override
public int getServerPort() {
// TODO Auto-generated method stub
return 0;
}

@Override
public boolean isSecure() {
// TODO Auto-generated method stub
return false;
}

@Override
public void removeAttribute(String arg0) {
// TODO Auto-generated method stub

}

@Override
public void setAttribute(String arg0, Object arg1) {
// TODO Auto-generated method stub

}

@Override
public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException {
// TODO Auto-generated method stub

}

}


/***============TestRequest 结束==================****/

/***============ExtHttpDoradoContext开始==============*****/
package com.bstek.dorado.common;

import javax.servlet.http.HttpServletRequest;

import com.bstek.scm.TestRequest;

public class ExtHttpDoradoContext extends HttpDoradoContext {

private final HttpServletRequest request;

public ExtHttpDoradoContext(){
request = new TestRequest();
}

public HttpServletRequest getRequest() {
return request;
}

public void setRequest(HttpServletRequest arg0) {
}
}

/***============ExtHttpDoradoContext结束==============*****/


/****==========业务调用开始===========****/
@Test
public void reverseFinanceBill_collectMoney() throws Exception{
try{
TestRequest.registerDoradoContext();
SessionHolder.setUserCode("kh0001");
SessionHolder.setUserUid(1L);
SessionHolder.setUserName("客户1");
/***测试代码****/

}finally{
TestRequest.unregisterDoradoContext();
}
}
/****==========业务调用结束===========****/

你可能感兴趣的:(单元测试怎样设值到session 中)