• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • 軟件測試中單元測試之Action的測試

    發表于:2010-03-03來源:作者:點擊數: 標簽:軟件測試單元Actionaction
    軟件測試 中 單元測試 之Action的測試 Action的測試是比較辛苦的。因為它依賴與其他的環境(比如 tomcat )。 在我的印象中,基于struts的測試是很麻煩的,因為對于execute方法,你必須mock兩個對象進去。 還好?;赪ebwork的測試相對簡單些。 下面讓我們來

    軟件測試單元測試之Action的測試

    Action的測試是比較辛苦的。因為它依賴與其他的環境(比如tomcat)。
    在我的印象中,基于struts的測試是很麻煩的,因為對于execute方法,你必須mock兩個對象進去。
    還好?;赪ebwork的測試相對簡單些。
    下面讓我們來測試一個例子吧

    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,一個測試的例子就好了。

    原文轉自:http://www.kjueaiud.com

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>