Struts2 Action的单元测试

Ora ·
更新时间:2024-11-10
· 544 次阅读

  对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

package model;

public class Account {     private String userName;     private String password;

    public Account() {     }

    public Account(String userName, String password) {         this.userName = userName;         this.password = password;     }

    public String getUserName() {         return userName;     }

    public void setUserName(String userName) {         this.userName = userName;     }

    public String getPassword() {         return password;     }

    public void setPassword(String password) {         this.password = password;     } }

  AccountAction.java

package action;

import com.opensymphony.xwork2.ActionSupport; import model.Account;

import java.util.logging.Logger;

public class AccountAction extends ActionSupport{

    private Account accountBean;     public String execute() throws Exception {         return SUCCESS;     }

    public void validate(){         if (accountBean.getUserName().length()==0){             addFieldError("accountBean.userName","User name is required.");         }

        if (accountBean.getUserName().length()<5){             addFieldError("accountBean.userName","User name must be at least 5 characters long.");         }

        if (accountBean.getUserName().length()>10){             addFieldError("accountBean.userName","User name cannot be at more thant 10 characters long.");         }     }

    public Account getAccountBean() {         return accountBean;     }

    public void setAccountBean(Account accountBean) {         this.accountBean = accountBean;     } }

  测试类:

  TestAccountAction.java

package ut;

import action.AccountAction; import com.opensymphony.xwork2.ActionProxy; import com.opensymphony.xwork2.config.ConfigurationProvider; import org.apache.struts2.StrutsTestCase;

import static org.testng.AssertJUnit.*;

public class TestAccountAction extends StrutsTestCase {     private AccountAction action;     private ActionProxy proxy;

    private void init() {         proxy = getActionProxy("/createaccount"); //action url,可以写扩展名".action"也可以干脆不写         action = (AccountAction) proxy.getAction();     }

    public void testUserNameErrorMessage() throws Exception {         request.setParameter("accountBean.userName", "Bruc");         request.setParameter("accountBean.password", "test");

        init();         proxy.execute();

        assertTrue("Problem There were no errors present in fieldErrors but there should have been one error present",                 action.getFieldErrors().size() == 1);         assertTrue("Problem field account.userName not present in fieldErrors but it should have been",                 action.getFieldErrors().containsKey("accountBean.userName"));     }

    public void testUserNameCorrect() throws Exception{         request.setParameter("accountBean.userName", "Bruce");         request.setParameter("accountBean.password", "test");

        init();         String result=proxy.execute();

        assertTrue("Problem There were errors present in fieldErrors but there should not have been any errors present",                 action.getFieldErrors().size()==0);

        assertEquals("Result returned form executing the action was not success but it should have been.",                 "success", result);

    } }

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

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

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"         http://struts.apache.org/dtds/struts-2.0.dtd> <struts>     <package name="testit" namespace="/" extends="struts-default">         <action name="createaccount" class="action.AccountAction">             <result name="success">/index.jsp</result>             <result name="input">/createaccount.jsp</result>         </action>     </package> </struts>

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

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

  MyInterceptor.java

package interceptor;

import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class MyInterceptor extends AbstractInterceptor{

    public String intercept(ActionInvocation actionInvocation) throws Exception {         System.out.println("before processing");        String rst= actionInvocation.invoke();         System.out.println("bye bye "+actionInvocation.getProxy().getMethod());         return rst;     } }

  修改一下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

  都是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

package action;

import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import service.MathService;

public class MathAction extends ActionSupport{     private MathService service;

    public String execute() throws Exception {         ServletActionContext.getRequest().setAttribute("add.result",service.add(1,2));         return SUCCESS;     }

    public MathService getService() {         return service;     }

    public void setService(MathService service) {         this.service = service;     } }

  MathService.java

package service;

public class MathService {     public int add(int a,int b){         return a+b;     } }

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

import action.MathAction; import com.opensymphony.xwork2.ActionProxy; import org.apache.struts2.StrutsSpringTestCase;

public class TestMathAction  extends StrutsSpringTestCase{     private MathAction action;     private ActionProxy proxy;

    protected String getContextLocations() {         return "spring/applicationContext.xml";     }

    private void init(){         proxy=getActionProxy("/add");         action=(MathAction)proxy.getAction();     }     public void testAdd() throws Exception{         init();         proxy.execute();         assertEquals(request.getAttribute("add.result"),3);     } }

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



单元 struts action struts2 单元测试 测试

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章