Struts2 Action的单元测试

对Struts2进行单元测试,以struts 2.2.1.1为例 ,可以使用struts2发行包中的struts2-junit-plugin-2.2.1.1.jar,它里面提供了两个类StrutsTestCase、StrutsSpringTestCase,分别提供对纯struts应用和struts+spring整合时的单元测试支持。下面分别说明。

1.StrutsTestCase

   首先准备一个纯struts2工程,建立工程过程略,但有如下的类:

   Account.java,是bean

  

  1. package model; 
  2.  
  3. public class Account { 
  4.     private String userName; 
  5.     private String password; 
  6.  
  7.     public Account() { 
  8.     } 
  9.  
  10.     public Account(String userName, String password) { 
  11.         this.userName = userName; 
  12.         this.password = password; 
  13.     } 
  14.  
  15.     public String getUserName() { 
  16.         return userName; 
  17.     } 
  18.  
  19.     public void setUserName(String userName) { 
  20.         this.userName = userName; 
  21.     } 
  22.  
  23.     public String getPassword() { 
  24.         return password; 
  25.     } 
  26.  
  27.     public void setPassword(String password) { 
  28.         this.password = password; 
  29.     } 

   AccountAction.java

  

  1. package action; 
  2.  
  3. import com.opensymphony.xwork2.ActionSupport; 
  4. import model.Account; 
  5.  
  6. import java.util.logging.Logger; 
  7.  
  8. public class AccountAction extends ActionSupport{ 
  9.  
  10.     private Account accountBean; 
  11.     public String execute() throws Exception { 
  12.         return SUCCESS; 
  13.     } 
  14.  
  15.     public void validate(){ 
  16.         if (accountBean.getUserName().length()==0){ 
  17.             addFieldError("accountBean.userName","User name is required."); 
  18.         } 
  19.  
  20.         if (accountBean.getUserName().length()<5){ 
  21.             addFieldError("accountBean.userName","User name must be at least 5 characters long."); 
  22.         } 
  23.  
  24.         if (accountBean.getUserName().length()>10){ 
  25.             addFieldError("accountBean.userName","User name cannot be at more thant 10 characters long."); 
  26.         } 
  27.     } 
  28.  
  29.     public Account getAccountBean() { 
  30.         return accountBean; 
  31.     } 
  32.  
  33.     public void setAccountBean(Account accountBean) { 
  34.         this.accountBean = accountBean; 
  35.     } 

   测试类:

  TestAccountAction.java

  

  1. package ut; 
  2.  
  3. import action.AccountAction; 
  4. import com.opensymphony.xwork2.ActionProxy; 
  5. import com.opensymphony.xwork2.config.ConfigurationProvider; 
  6. import org.apache.struts2.StrutsTestCase; 
  7.  
  8. import static org.testng.AssertJUnit.*; 
  9.  
  10.  
  11. public class TestAccountAction extends StrutsTestCase { 
  12.     private AccountAction action; 
  13.     private ActionProxy proxy; 
  14.  
  15.     private void init() { 
  16.         proxy = getActionProxy("/createaccount"); //action url,可以写扩展名".action"也可以干脆不写 
  17.         action = (AccountAction) proxy.getAction(); 
  18.     } 
  19.  
  20.     public void testUserNameErrorMessage() throws Exception { 
  21.         request.setParameter("accountBean.userName", "Bruc"); 
  22.         request.setParameter("accountBean.password", "test"); 
  23.  
  24.         init(); 
  25.         proxy.execute(); 
  26.  
  27.         assertTrue("Problem There were no errors present in fieldErrors but there should have been one error present"
  28.                 action.getFieldErrors().size() == 1); 
  29.         assertTrue("Problem field account.userName not present in fieldErrors but it should have been"
  30.                 action.getFieldErrors().containsKey("accountBean.userName")); 
  31.     } 
  32.  
  33.     public void testUserNameCorrect() throws Exception{ 
  34.         request.setParameter("accountBean.userName", "Bruce"); 
  35.         request.setParameter("accountBean.password", "test"); 
  36.  
  37.         init(); 
  38.         String result=proxy.execute(); 
  39.  
  40.         assertTrue("Problem There were errors present in fieldErrors but there should not have been any errors present"
  41.                 action.getFieldErrors().size()==0); 
  42.  
  43.         assertEquals("Result returned form executing the action was not success but it should have been."
  44.                 "success", result); 
  45.  
  46.     } 

   测试逻辑比较简单,action中的validate方法会保证用户名长度在5--9之间。

   定义struts.xml,放在类路径的根目录下,而非web-inf/classes下,否则会找不到,不会加载你定义的内容。

  

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2.  
  3. <!DOCTYPE struts PUBLIC 
  4.         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
  5.         "http://struts.apache.org/dtds/struts-2.0.dtd"
  6. <struts> 
  7.     <package name="testit" namespace="/" extends="struts-default"
  8.         <action name="createaccount" class="action.AccountAction"
  9.             <result name="success">/index.jsp</result> 
  10.             <result name="input">/createaccount.jsp</result> 
  11.         </action> 
  12.     </package
  13. </struts> 

   至于action/result的定义中用到的jsp页面,不必真实存在,保持不为空就行,否则,action测试的时候,会说result未定义之类的错误,因为此测试会模拟action真实状态下的运行。运行,一切OK。

   正因为会模拟真实状态下的运行,所以拦截器也会正常被触发,下面再定义一个拦截器测试一下:

   MyInterceptor.java

  

  1. package interceptor; 
  2.  
  3. import com.opensymphony.xwork2.ActionInvocation; 
  4. import com.opensymphony.xwork2.interceptor.AbstractInterceptor; 
  5.  
  6. public class MyInterceptor extends AbstractInterceptor{ 
  7.  
  8.     public String intercept(ActionInvocation actionInvocation) throws Exception { 
  9.         System.out.println("before processing"); 
  10.        String rst= actionInvocation.invoke(); 
  11.         System.out.println("bye bye "+actionInvocation.getProxy().getMethod()); 
  12.         return rst; 
  13.     } 

  

   修改一下struts.xml,加入拦截器的定义:

   <package name="testit" namespace="/" extends="struts-default">
       <interceptors>
            <interceptor name="testInterceptor" class="interceptor.MyInterceptor"/>
        </interceptors>
        <action name="createaccount" class="action.AccountAction">
            <result name="success">/index.jsp</result>
            <result name="input">/createaccount.jsp</result>
            <interceptor-ref name="defaultStack"/>
            <interceptor-ref name="testInterceptor"/>
        </action>
    </package>

   运行,控制台会输出:

   before processing

   bye bye execute

    使用的jar包如下图:

  

   都是struts发行包提供的,其它不相关的jar不要加,尤其是以plugin.jar结尾的文件,更不要加struts2-spring-plugin-2.2.1.1.jar,加了会加载相关的东西,但这里却提供不了,导致测试无法运行。实际spring-beans-2.5.6.jar和spring-context-2.5.6.jar也不是必须的,但加了也无所谓,在StrutsSpringTestCase是需要的。另外,web.xml不需要配置,根本不会去这里找配置信息。

  

2.StrutsSpringTestCase

  这个和前面的过程类似,需要的类分别如下。

  MathAction.java

 

  1. package action; 
  2.  
  3. import com.opensymphony.xwork2.ActionContext; 
  4. import com.opensymphony.xwork2.ActionSupport; 
  5. import org.apache.struts2.ServletActionContext; 
  6. import service.MathService; 
  7.  
  8. public class MathAction extends ActionSupport{ 
  9.     private MathService service; 
  10.  
  11.     public String execute() throws Exception { 
  12.         ServletActionContext.getRequest().setAttribute("add.result",service.add(1,2)); 
  13.         return SUCCESS; 
  14.     } 
  15.  
  16.     public MathService getService() { 
  17.         return service; 
  18.     } 
  19.  
  20.     public void setService(MathService service) { 
  21.         this.service = service; 
  22.     } 

  MathService.java

 

  1. package service; 
  2.  
  3. public class MathService { 
  4.     public int add(int a,int b){ 
  5.         return a+b; 
  6.     } 

  测试类TestMathAction,测试一下MathService.add是否能正确地返回两个数相加的值。

 

  1. import action.MathAction; 
  2. import com.opensymphony.xwork2.ActionProxy; 
  3. import org.apache.struts2.StrutsSpringTestCase; 
  4.  
  5. public class TestMathAction  extends StrutsSpringTestCase{ 
  6.     private MathAction action; 
  7.     private ActionProxy proxy; 
  8.  
  9.     protected String getContextLocations() { 
  10.         return "spring/applicationContext.xml"
  11.     } 
  12.  
  13.     private void init(){ 
  14.         proxy=getActionProxy("/add"); 
  15.         action=(MathAction)proxy.getAction(); 
  16.     } 
  17.     public void testAdd() throws Exception{ 
  18.         init(); 
  19.         proxy.execute(); 
  20.         assertEquals(request.getAttribute("add.result"),3); 
  21.     } 

  这里有一个小trick,默认情况下,applicationContext.xml也要放在classpath的根目录下,但如果项目需要不放在那里,就要覆盖getContextLocations方法返回其class path,开头可以有也可以没有“/”,这里我放在包spring下,所以就返回spring/applicationContext.xml,至于struts和spring整合的配置就不用写了,想必大家都会。需要的jar在上面的基础上,加入struts2-spring-plugin-2.2.1.1.jar就行了,对了,两种测试都需要jsp-api.jarservlet-api.jar,去tomcat里copy一份即可,junit.jar也是需要的(废话?!)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值