如何测试struts2的action?

在web应用中做action的单元测试,一向比较麻烦.因为它对web容器的依赖。现在有办法了。struts提供了 个测试插件,结合JUnit,可以很方便的进行单元测试,
一、加入struts2-junit-plugin.xxx.jar插件
二、测试类

/**
*
*/
package com.auth;

import org.apache.struts2.StrutsSpringTestCase;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.auth.service.UserManager;
import com.auth.web.action.UserAction;
import com.opensymphony.xwork2.ActionProxy;


/**
* @author llf
*
*/
public class UserActionTest extends StrutsSpringTestCase{
private ActionProxy proxy;
private UserAction action;
private UserManager manager;
@Override
public void setUp() throws Exception{
super.setUp(); //必须
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});
manager = (UserManager)ctx.getBean("userManager");
}

@Test
public void testAction() throws Exception{
proxy = this.getActionProxy("/user_list.action");
action = (UserAction)proxy.getAction();
action.setManager(manager);
String result = action.list();
assertEquals("success",result);
}
}




三、注意
1、strut2-junit-plugin共提供了两个基类:
StrutsTestCase:供普通action继承
StrutsSpringTestCase:供ssh环境中的action继承

2、在ssh环境中一定要super.setUp()

3、action中的注入可以手动用setXxx()方法实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值