struts2.1之后的Junit plugin使用

 虽然我不完全是测试驱动开发的信徒,但是我很认可这种做法,可能觉得自己写的代码底气不足,所以老是希望能够通过测试来加强自己的信心。struts2提供了一个与JUNIT集成的插件,但是只支持2.1之后的版本。没办法,下了个比较新的版本的struts2来学习一下,还真不错。

       最新版本的struts2需要的基础包是下面几个:commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,commons-logging-1.0.4.jar,freemarker-2.3.15.jar,ognl-2.7.3.jar,struts2-core-2.1.8.1.jar,xwork-core-2.1.6.jar,想要集成单元测试就需要加入下面的包:spring-core-2.5.6.jar,spring-test-2.5.6.jar,struts2-junit-plugin-2.1.8.1.jar,这三者有个依赖关系,由于这个junit插件要用到spring的mock对象request、response、servletContext,所以需要spring-test这个包,但是spring的工具当然需要spring-core这个包。
    把这些包加进去之后,可以如下写你的测试类,测试类一定要继承StrutsTestCase,这样你就可以使用这些mock对象 了。
 import org.apache.struts2.StrutsTestCase;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionProxy;

 
public class StrutsTest extends StrutsTestCase {

 
          //对action的测试
public void testGetActionMapping() throws Exception {
ActionMapping mapping = getActionMapping("/begin/helloWorld.action");
assertNotNull(mapping);
assertEquals("/begin", mapping.getNamespace());
assertEquals("helloWorld", mapping.getName());

 
}

 
    //对action的执行过程的一个模拟测试
public void testGetActionProxy() throws Exception {
  //set parameters before calling getActionProxy
request.setParameter("username", "FD");
ActionProxy proxy = getActionProxy("/begin/helloWorld.action");
assertNotNull(proxy);

 
HelloWorld action = (HelloWorld) proxy.getAction();
assertNotNull(action);

 
String result = proxy.execute();
assertEquals(Action.SUCCESS, result);
assertEquals("FD", action.getUsername());
}
}
下面是我的action类:
import com.opensymphony.xwork2.ActionSupport;

 
@SuppressWarnings("serial")
public class HelloWorld extends ActionSupport {

 
private String username;

 
public String getUsername() {
return username;
}

 
public void setUsername(String username) {
this.username = username;
}

 
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}

 
}
配置文件struts.xml的内容:
<?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="test" namespace="/begin" extends="struts-default">
        <action name="helloWorld" class="com.gzjp.pms.begin.HelloWorld">
        <result>/welcome.jsp</result>
        </action>
    </package>
</struts>
下面还有一些可以用来进行单元测试的方法和mock对象。
Method NameDescription
executeAction(String)Pass the url for the action, and it will return the output of the action. This output is not the action result, like "success", but what would be written to the result stream. To use this the actions must be using a result type that can be read from the classpath, like FreeMarker, velocity, etc (if you are using the experimental Embedded JSP Plugin, you can use JSPs also)
getActionProxy(String)Builds an action proxy that can be used to invoke an action, by calling execute() on the returned proxy object. The return value of execute() is the action result, like "success"
getActionMapping(String)Gets an ActionMapping for the url
injectStrutsDependencies(object)Injects Struts dependencies into an object (dependencies are marked with Inject)
findValueAfterExecute(String)Finds an object in the value stack, after an action has been executed
FieldDescription
MockHttpServletRequest requestThe request that will be passed to Struts. Make sure to set parameters in this object before calling methods like getActionProxy
MockHttpServletResponse responseThe response object passed to Struts, you can use this class to test the output, response headers, etc
MockServletContext servletContextThe servlet context object passed to Struts

转自:http://kang36897.blog.163.com/blog/static/1704737320107109311267/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值