VIRGIN FOREST OF JAVA
不要埋头苦干,要学习,学习,再学习。。。。。
powered by R.Zeus
mock是个模拟测试的东西,她创造的东西都是假的,在数据库测试,网络测试中可以 用的很爽!

public class MockPrototypeTestCase extends org.jmock.MockObjectTestCase {
    Mock accountService = null;

    MockPrototype mo = new MockPrototype();
    protected void setUp() throws Exception {
        super.setUp();
        accountService = new Mock(AccountService.class);
//在MockPrototype 要用到AccountService.class,所以这里创建了一个,不过这个不是真实的,是模拟的
        mo.setAccountService((AccountService) accountService.proxy());
    }
    protected void tearDown() throws Exception {
        super.tearDown();
        mo = null;
    }
    public void testloginProcess() {
        Account a = new Account();
        a.setLoginId("aa");
        a.setPassword(StringUtil.encodePassword("bb", Constants.PASSWORD_ENCODING_TYPE));
//这里模拟了      getAccount,即getAccount("aa"),返回a;在mock中没有真实的东西,都是模拟的!
  accountService.expects(atLeastOnce()) //注意 atLeastOnce()这个参数,这个指定使用次数,如果是
                .method("getAccount")             //atLeastOnce,则这个method中则至少使用一次,否则会报错!
                .with(eq("aa"))                      //总之,按照定义的函数调用,同时必须遵守规定,如次数!
                .will(returnValue(a));            //在mo的loginProcess()中,会用到这些方法
        accountService.expects(atLeastOnce())
                .method("getAccount")
                .with(eq("cc"))
                .will(returnValue(null));
        mo.loginProcess("aa","bb");
        try {
            mo.loginProcess("cc","bb");
        } catch (FrameworkdemoServiceException e) {
            assertEquals(e.getMessage(),"null.lll");  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
posted on 2006-08-04 18:56 R.Zeus 阅读(357) 评论(0)  编辑  收藏 所属分类: SPRINGTest

只有注册用户登录后才能发表评论。


网站导航: