zoukankan      html  css  js  c++  java
  • struts2 action测试用例

    struts2 action测试用例  

    http://hezhong002.iteye.com/blog/1108645

    这篇文章主要内容是使用junit对struts2的action进行测试。

    必须的包:

    spring-core-2.5.6.jar

    spring-test-2.5.6.jar

    struts2-junit-plugin-2.2.1.1.jar

    注:测试action ,需用到spring。

    主要的测试方法:

    测试用例需继承 StrutsTestCase

    重写setupBeforeInitDispatcher方法,相当与Beforeclass

    方法描叙
    executeAction(String)

    根据action的URL链接,输出action的结果流数据(结果类型不是SUCCESS,可以是FreeMarker, velocity, JSP等)

    getActionProxy(String)生成一个action的代理
    injectStrutsDependencies(object)注入一个依赖对象
    getActionMapping(String)获得ActionMapping
    findValueAfterExecute(String)action执行之后,从值栈中取值
      
    属性描叙
    MockHttpServletRequest requestrequset传递给struts2
    MockHttpServletResponse response用于测试struts2输出
    MockServletContext servletContexThe servlet context 传递给struts2
      

    用例:

    Java代码 复制代码 收藏代码
    1. public class TestAction extends ActionSupport {   
    2.     private String name;   
    3.   
    4.     public String getName() {   
    5.         return name;   
    6.     }   
    7.   
    8.     public void setName(String name) {   
    9.         this.name = name;   
    10.     }   
    11. }  

    junit:

    Java代码 复制代码 收藏代码
    1. package org.apache.struts2;   
    2.   
    3. import org.apache.struts2.dispatcher.mapper.ActionMapping;   
    4.   
    5. import java.util.HashMap;   
    6. import java.io.UnsupportedEncodingException;   
    7.   
    8. import com.opensymphony.xwork2.ActionProxy;   
    9. import com.opensymphony.xwork2.Action;   
    10.   
    11. import javax.servlet.ServletException;   
    12.   
    13. public class StrutsTestCaseTest extends StrutsTestCase {   
    14.     public void testGetActionMapping() {   
    15.         ActionMapping mapping = getActionMapping("/test/testAction.action");   
    16.         assertNotNull(mapping);   
    17.         assertEquals("/test", mapping.getNamespace());   
    18.         assertEquals("testAction", mapping.getName());   
    19.     }   
    20.   
    21.     public void testGetActionProxy() throws Exception {   
    22.         //set parameters before calling getActionProxy   
    23.         request.setParameter("name""FD");   
    24.            
    25.         ActionProxy proxy = getActionProxy("/test/testAction.action");   
    26.         assertNotNull(proxy);   
    27.   
    28.         TestAction action = (TestAction) proxy.getAction();   
    29.         assertNotNull(action);   
    30.   
    31.         String result = proxy.execute();   
    32.         assertEquals(Action.SUCCESS, result);   
    33.         assertEquals("FD", action.getName());   
    34.     }   
    35.   
    36.     public void testExecuteAction() throws ServletException, UnsupportedEncodingException {   
    37.         String output = executeAction("/test/testAction.action");   
    38.         assertEquals("Hello", output);   
    39.     }   
    40.   
    41.     public void testGetValueFromStack() throws ServletException, UnsupportedEncodingException {   
    42.         request.setParameter("name""FD");   
    43.         executeAction("/test/testAction.action");   
    44.         String name = (String) findValueAfterExecute("name");   
    45.         assertEquals("FD", name);   
    46.     }   
    47. }  

    分享到:
    评论

    2 楼 hezhong002 2011-12-21  
    iyuxuan 写道
    请教一下,这个能把写进数据库的数据在方法执行完之后回滚掉吗?
    如果能的话,能否给出点提示啊

    功能测试中数据库操作是写mock对象的,
    如果你要执行真实的数据库操作,数据是不会回滚的。

    1 楼 iyuxuan 2011-12-20  
    请教一下,这个能把写进数据库的数据在方法执行完之后回滚掉吗?
    如果能的话,能否给出点提示啊
  • 相关阅读:
    python函数收集不确定数量的值
    PHP比较数组、对象是否为空
    PHP实现斐波那契数列
    Python之复制列表
    1004. 最大连续1的个数 III(滑动窗口)
    276. 栅栏涂色(动态规划)
    376. 摆动序列
    148. 排序链表
    143. 重排链表
    1530. 好叶子节点对的数量
  • 原文地址:https://www.cnblogs.com/lexus/p/2340707.html
Copyright © 2011-2022 走看看