zoukankan      html  css  js  c++  java
  • Struts2 不依赖Spring 的测试方式

    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);
        }

    }

  • 相关阅读:
    OpenCV -- VideoWriter
    Opencv -- 显示创建Mat对象的七种方式
    OpenCV -- Video Capture
    OpenCV -- Mat类详解
    OpenCV -- .at<uchar>(j, i) 和.at<uchar>(Point(j, i)) 的区别
    OpenCV -- 图像遍历的四种方式(at、指针、isCountinuous、迭代器)、在Vector尾部加数据函数push_back()
    css实现上传按钮
    理解原型对象
    margin 0 auto 元素元素并未居中的原因!
    css中的布局
  • 原文地址:https://www.cnblogs.com/lexus/p/2340703.html
Copyright © 2011-2022 走看看