单元测试之实践四 Action的测试
来源:互联网 发布:网络推广文案 编辑:程序博客网 时间:2024/06/02 09:01
Action的测试是比较辛苦的。因为它依赖与其他的环境(比如tomcat)。
在我的印象中,基于struts的测试是很麻烦的,因为对于execute方法,你必须mock两个对象进去。
还好。基于Webwork的测试相对简单些。
下面让我们来测试一个例子吧
有经验的程序员见到上面的代码应该就知道怎么测试了。
我们只需setAccount,跟setAccountService即可,
而Account本身来讲就是是个po,所以可以自己new一个
AccountService则可以mock一个。真是太完美了,我太喜好mock,它总是给我惊喜
ok,一个测试的例子就好了。
在我的印象中,基于struts的测试是很麻烦的,因为对于execute方法,你必须mock两个对象进去。
还好。基于Webwork的测试相对简单些。
下面让我们来测试一个例子吧
java 代码
- Account account;
- IAccountService accountService;
- public void setAccount(Account account) {
- this.account = account;
- }
- public void setAccountService(IAccountService accountService) {
- this.accountService = accountService;
- }
- public String regist() throws Exception {
- if(account == null) {
- account = new Account();
- return INPUT;
- }
- if(!validForm(account))
- return INPUT;
- try {
- accountService.regist(account);
- } catch (ObjectExistsException e) {
- e.printStackTrace();
- return INPUT;
- }
- return SUCCESS;
- }
- private boolean validForm(Account e) {
- if(e.getName() == null || e.getName().trim().equals(""))
- return false;
- if(e.getPassword() == null || e.getPassword().trim().equals(""))
- return false;
- return true;
- }
有经验的程序员见到上面的代码应该就知道怎么测试了。
我们只需setAccount,跟setAccountService即可,
而Account本身来讲就是是个po,所以可以自己new一个
AccountService则可以mock一个。真是太完美了,我太喜好mock,它总是给我惊喜
java 代码
- package org.wuhua.action;
- import junit.framework.TestCase;
- import org.easymock.MockControl;
- import org.wuhua.exception.ObjectExistsException;
- import org.wuhua.model.Account;
- import org.wuhua.service.IAccountService;
- import sms.king.AccountManager;
- import com.opensymphony.xwork.Action;
- public class AccountActionTest extends TestCase {
- private MockControl control;
- IAccountService accountService;
- protected void setUp() throws Exception {
- control = MockControl.createControl(IAccountService.class);
- accountService = (IAccountService) control.getMock();
- }
- public void testRegistOk() throws Exception {
- Account employee = new Account("name");
- employee.setPassword("password");
- accountService.regist(employee);
- control.setVoidCallable(1);
- control.replay();
- AccountAction action = new AccountAction();
- action.setAccount(employee);
- action.setAccountService(accountService);
- assertEquals(Action.SUCCESS, action.regist());
- control.verify();
- }
- public void testRegistNameExists() throws Exception {
- Account employee = new Account("name");
- employee.setPassword("password");
- accountService.regist(employee);
- control.setThrowable(new ObjectExistsException(""));
- control.replay();
- AccountAction action = new AccountAction();
- action.setAccount(employee);
- action.setAccountService(accountService);
- assertEquals(Action.INPUT, action.regist());
- control.verify();
- }
- }
ok,一个测试的例子就好了。
- 单元测试之实践四 Action的测试
- 单元测试之实践三 Service的测试
- 单元测试之实践二,关于DAO的测试
- Visual Studio 单元测试之四---Generic测试
- 单元测试之Service的测试
- MVC与单元测试实践之健身网站(四)-动作管理
- spring做单元测试,包括对struts2的action 的测试。
- Struts2 Action的单元测试
- Struts2 Action的单元测试
- Struts2 Action的单元测试
- Struts2 Action的单元测试
- Struts2 Action的单元测试
- Struts2 Action的单元测试
- Visual Studio 单元测试之四---Generic测试 (转)
- [转]Visual Studio 单元测试之四---Generic测试
- 谈谈单元测试之(四):测试工具 TestNG
- 软件测试系列之四 单元测试(Junit)
- 谈谈单元测试之(四):测试工具 TestNG
- ASP.NET2.0打通文件图片处理任督二脉
- test
- 天苍苍野茫茫
- 单元测试之实践三 Service的测试
- 并行计算简介和多核CPU编程Demo
- 单元测试之实践四 Action的测试
- ORACLE联机日志文件丢失或损坏的处理方法
- 开张了 !!!!!!!!
- Request接受中文
- rootkit.agent.pg病毒怎么杀
- HTML生成JS 编辑器
- 能看见风了
- 一個安裝SQL2005必須注意的問題(VS2005先装上再裝SQL2005,一般就有这个问题了)
- ffmpeg原来如此