http://hi.baidu.com/%C9%AE_%CC%C6/blog/item/3560e91af41f92078618bf84.html
2011-12-23 20:40
package cn.wang.myhome.struts2.action.test;
import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map;
import javax.servlet.ServletException;
import org.apache.struts2.StrutsTestCase; import org.apache.struts2.dispatcher.mapper.ActionMapping; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.mock.web.MockHttpSession;
import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionProxy;
public class TestLoginAction extends StrutsTestCase { @BeforeClass public void testBeforeClass() throws Exception { System.out.println("==>执行setUp方法"); super.setUp(); Map<String, Object> sessionMap = new HashMap<String, Object>(); MockHttpSession session = new MockHttpSession(); request.setSession(session); ActionContext.getContext().setSession(sessionMap); Map<String, Object> session2 = ActionContext.getContext().getSession(); System.out.println("===>" + session2); }
/** * 测试映射文件 */ @Test public void testGetActionMapping() { ActionMapping mapping = getActionMapping("/login.action"); assertNotNull(mapping); assertEquals("/", mapping.getNamespace()); assertEquals("login", mapping.getName()); }
/** * 测试 获得ActionProxy * * @throws Exception */ @Test public void testGetActionProxy() throws Exception { Map<String, Object> sessionMap = new HashMap<String, Object>(); // request.setSession(session); // set parameters before calling getActionProxy request.setParameter("name", "wang"); request.setParameter("password", "123456"); // assertNotNull(action);//断言可获得Action
ActionProxy proxy = getActionProxy("login"); // //////////////////!!!!!!!!!!!!//////////////////////// ActionContext.getContext().setSession(sessionMap);// 必须要在获得代理后 // 设置session // //////////////////!!!!!!!!!!!!//////////////////////// assertNotNull(proxy);// 断言可获得代理 // LoginAction action = (LoginAction) proxy.getAction(); System.out.println("执行代理前的session" + ActionContext.getContext().getSession()); String result = proxy.execute(); assertEquals(Action.SUCCESS, result); // assertEquals("FD", action.getName()); }
/** * 测试execute * * @throws ServletException * @throws UnsupportedEncodingException */ @Test public void testExecuteAction() throws ServletException, UnsupportedEncodingException { String output = executeAction("/login.action"); System.out.println("===>" + output + "==>"); assertEquals("input", output); }
/** * 测试值栈信息 * * @throws ServletException * @throws UnsupportedEncodingException */ @Test public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException { // request.setParameter("name", "FD"); executeAction("/test/testAction.action"); String name = (String) findValueAfterExecute("name"); assertEquals("FD", name); }
} |