struts2 action测试用例  

http://hezhong002.iteye.com/blog/1108645
    博客分类:
  • java

这篇文章主要内容是使用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 request requset传递给struts2
MockHttpServletResponse response 用于测试struts2输出
MockServletContext servletContex The servlet context 传递给struts2
   

 

用例:

 

Java代码 struts2 action测试用例 struts2 action测试用例struts2 action测试用例
  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代码 struts2 action测试用例 struts2 action测试用例struts2 action测试用例
  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. }  

 

 

 

相关文章:

  • 2022-02-27
  • 2021-09-02
  • 2022-12-23
  • 2021-08-23
  • 2022-12-23
  • 2021-10-13
  • 2021-12-17
  • 2022-12-23
猜你喜欢
  • 2022-01-05
  • 2021-06-19
  • 2021-08-01
  • 2021-07-12
  • 2021-12-14
  • 2021-09-13
相关资源
相似解决方案