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

    }

  • 相关阅读:
    如何使用IntelliJ IDEA 14创建基于Maven3的Java Web Project
    一些python语法的合集
    thuwc2019滚粗记
    noip2018总结
    树链剖分
    NOIp2017游记(滚粗之旅)
    使用Redux管理你的React应用
    校招面试中积累的前端问题
    WebService 生成类的命令语句
    Request 请求页面的地址路径获取
  • 原文地址:https://www.cnblogs.com/lexus/p/2340703.html
Copyright © 2011-2022 走看看