struts2.1之后的Junit plugin使用

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

struts2.1之后的Junit plugin使用   

2010-08-10 09:50:53|  分类: J2EE |  标签: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 Name Description
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
Field Description
MockHttpServletRequest request The request that will be passed to Struts. Make sure to set parameters in this object before calling methods like getActionProxy
MockHttpServletResponse response The response object passed to Struts, you can use this class to test the output, response headers, etc
MockServletContext servletContext

你可能感兴趣的:(struts2)