好文档 - 专业文书写作范文服务资料分享网站

spring快速入门教程详解 - 图文 

天下 分享 时间: 加入收藏 我要投稿 点赞

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

对象struts Action进行单元测试

业务代理和DAO都起作用,我们看看MVC框架吸盘(sucker)的上部。停!不止这些吧,你可以进行C(Controller),不是V(View)。为了管理用户建一个Struts Action,继续进行驱动测试(Test-Driven)开发。

Equinox是为Struts配置的。配置Struts需要在web.xml 中进行一些设置,并在web/WEB-INF下定义一个struts-config.xml文件。由于Struts开发人员比较多,这里先使用Struts。第4 章用Spring进行处理。你想跳过这一节,直接学习Spring MVC方法,请参考第4章:Spring s MVC Framework。

在test/org/appfuse/web目录下新建一个文件UserActionTest.java,开发你的第一个Struts Aciton单元测试。文件内容如下:

package org.appfuse.web;

// use your IDE to handle imports

public class UserActionTest extends MockStrutsTestCase {public UserActionTest(String testName) { super(testName); }

public void testExecute() { setRequestPathInfo(\/user\); addRequestParameter(\id\, \1\); actionPerform();

verifyForward(\success\); verifyNoActionErrors(); } }

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

为web层创建Action和Model(DynaActionForm)

1.在src/org/appfuse/web下新建一个文件UserAction.java。这个扩展了DispatchAction,你可以花几分钟,在这个类中,创建CRUD方法。

package org.appfuse.web;

// use your IDE to handle imports

public class UserAction extends DispatchAction {

private static Log log = LogFactory.getLog(UserAction.class); public ActionForward execute(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception{

request.getSession().setAttribute(\test\, \succeeded!\); log.debug(\looking up userId: \ + request.getParameter(\id\)); return mapping.findForward(\success\); } }

2.为了配置Struts,使”/user”这个请求路径代表其它。在web/WEB-INF/struts-config.xml中加入一个action-mapping。打开文件加入:

3.执行命令 ant test -Dtestcase=UserAction ,你会看到友好的“BUILDSUCCESSFUL” 信息。

4.在struts-config.xml中添加form-bean定义。对于Struts ActionForm,使用DynaActionForm,这是一个javabean,可以从XML定义中动态的创建。

这里没有使用具体的ActionForm,因为只使用一个User 对象的包装器。理想情况下,你可以User 对象,但会失去Struts环境下的一些特性:验证属性(validate

properties),checkbox复位(reset checkboxs)。后面,将演示用Spring怎幺会更加简单,

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

它可以让你在web层使用 User对象。

5.修改action定义,在request中使用这个form。

6.修改UserActionTest,测试不同的 CRUD方法。

public class UserActionTest extends MockStrutsTestCase { public UserActionTest(String testName) { super(testName); }

// Adding a new user is required between tests because HSQL creates // an in-memory database that goes away during tests. public void addUser() { setRequestPathInfo(\/user\);

addRequestParameter(\method\, \save\);

addRequestParameter(%user.firstName\, \Juergen\); addRequestParameter(%user.lastName\, \Hoeller\); actionPerform(); verifyForward(\list\); verifyNoActionErrors(); }

public void testAddAndEdit() { addUser();

// edit newly added user

addRequestParameter(\method\, \edit\); addRequestParameter(\id\, \1\); actionPerform();verifyForward(\edit\); verifyNoActionErrors(); }

public void testAddAndDelete() { addUser();

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

// delete new user

setRequestPathInfo(\/user\);

addRequestParameter(\method\, \delete\); addRequestParameter(%user.id\, \1\); actionPerform(); verifyForward(\list\); verifyNoActionErrors(); }

public void testList() { addUser();

setRequestPathInfo(\/user\);

addRequestParameter(\method\, \list\); actionPerform(); verifyForward(\list\); verifyNoActionErrors();

List users = (List) getRequest().getAttribute(%users\); assertNotNull(users);

assertTrue(users.size() == 1); } }

7.修改UserAction,这样测试程序才能通过,并能处理(客户端)请求。最简单的方法是添加edit,save和delete方法,请确保你事先已经删除了execute方法。下面是修改过的UserAction.java文件。

public class UserAction extends DispatchAction {

private static Log log = LogFactory.getLog(UserAction.class); private UserManager mgr = null;

public void setUserManager(UserManager userManager) { this.mgr = userManager; }

public ActionForward delete(ActionMapping mapping,

ActionForm form,

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

HttpServletRequest request,

HttpServletResponse response) throws Exception{

if (log.isDebugEnabled()) {

log.debug(\entering 'delete' method...\);

}

mgr.removeUser(request.getParameter(%user.id\)); ActionMessages messages = new ActionMessages();

messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(%user.deleted\));

saveMessages(request, messages);

return list(mapping, form, request, response); }

public ActionForward edit(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception {

if (log.isDebugEnabled()) {

log.debug(\entering 'edit' method...\); }

DynaActionForm userForm = (DynaActionForm) form; String userId = request.getParameter(\id\); // null userId indicates an add if (userId != null) {

User user = mgr.getUser(userId); if (user == null) {

ActionMessages errors = new ActionMessages();

errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(%user.missing\));

saveErrors(request, errors); return mapping.findForward(\list\); }

userForm.set(%user\, user); }

spring快速入门教程详解 - 图文 

DownloadsByhttp://down.liehuo.nethttp://www.elecfans.com电子发烧友http://bbs.elecfans.com电子技术论坛对象strutsAction进行单元测试业务代理和DAO都起作用,我们看看MVC框架吸盘(sucker)的上部。停!不止这些吧,你可以进行C(Controll
推荐度:
点击下载文档文档为doc格式
8eir722jc617c1937435
领取福利

微信扫码领取福利

微信扫码分享