java中expectations_JMockit使用实例<一>mock一个类的方法、Expectations

[    关键词:verifications 想验证被mock的类的某个方法是否被调用单元测试类清单 /** * 演示验证被mock的类的某个方法是否被调用 * @sina weibo regbin@tom.com */public

关键词:如何mock一个类的方法、expectations

源类清单

/** * 演示如何mock一个类的方法 * @sina weibo:regbin@tom.com */public class dateutil { private int type; public static final string getcurrentdatestr() { simpledateformat sdf = new simpledateformat("yyyy-mm-dd hh:mm:ss"); return sdf.format(dateutil.now()); } public static final string getcurrentdatestrbyformattype(int type) { if (type == 1) { simpledateformat sdf = new simpledateformat("yyyy/mm/dd hh:mm:ss"); return sdf.format(dateutil.now()); } else { return dateutil.getcurrentdatestr(); } } public static final date now() { return new date(); } public int gettype() { return type; } public void settype(int type) { this.type = type; }}

单元测试类清单

/** * 演示如何mock一个类的方法 */public class dateutiltest { /** * mock某个类方法 */ @test public void testgetcurrentdatestr() { //dateutil.class,要mock的类 new expectations(dateutil.class) { { //要mock的方法now,其他方法dateutil.class dateutil.now(); //期望方法返回的结果 result = mockdate(); } }; assert.assertequals("2010-07-22 15:52:55", dateutil.getcurrentdatestr()); } /** * mock 某个类方法根据不同参数返回不同值 */ @test public void testgetcurrentdatestrbyformattype() { new expectations(dateutil.class) { { dateutil.getcurrentdatestrbyformattype(anyint); result = new delegate() { public string getcurrentdatestrbyformattype(int type) { if (type == 1) { return "2010/07/22 15:52:55"; } else { return "2010-07-22 15:52:55"; } } }; } }; assert.assertequals("2010-07-22 15:52:55", dateutil.getcurrentdatestrbyformattype(2)); } public static date mockdate() { calendar c = calendar.getinstance(); c.set(2010, 6, 22, 15, 52, 55); return c.gettime(); }}

小结expectations:一个expectations块是给定测试方法中将会涉及到的mock项中,预期将要被调用的方法或构造函数。一个expectations可以包含多个预期的要执行方法(mock),但不必包含所有预期会被调用的方法。在expectations中;除了可以指定预期的方法外,还可以指定方法的参数的精确值或约束行为(满足某个断言);同时expectations中还可以指定该方法预期的返回值(如果有)或预期抛出的异常。expectations(.class){}这种方式只会模拟区域中包含的方法,这个类的其它方法将按照正常的业务逻辑运行,上面的例子,定义了一个mock类dateutil,同时在expectation中定义了预期会被调用的方法now,以及now方法的返回值,[关键词:如何mock一个类的方法、Expectations 源类清单   /** * 演示如何mock一个类的方法 * @sina weibo:regbin@tom.com */ public class DateUtil {这种方式还有种等价实现方式,使用@mocked标签

@test public void testgetcurrentdatestr(@mocked(methods="now")dateutil dateutil) { //dateutil.class,要mock的类 new expectations() { { //声明要mock的方法(注:其它方法按照正常的业务逻辑运行) dateutil.now(); //期望方法返回的结果 result = mockdate(); } }; assert.assertequals("2010-07-22 15:52:55", dateutil.getcurrentdatestr()); }

nonstrictexpectations:expectations块里声明的mock方法,是一定要被执行的,如果没有被执行,会认为整个测试case不通过;nonstrictexpectations就没有这个限制,看例子:

@test public void testgetcurrentdatestr(@mocked(methods="now")dateutil dateutil) { //dateutil.class,要mock的类 new nonstrictexpectations() { { //声明要mock的方法(注:其它方法按照正常的业务逻辑运行) dateutil.now(); //期望方法返回的结果 result = mockdate(); dateutil.gettype(); result = 1; } }; assert.assertequals("2010-07-22 15:52:55", dateutil.getcurrentdatestr()); }

dateutil.gettype()在后面的断言没用被调用,但也不会出错,但是如果把nonstrictexpectations换成expectations,就会出错,在expectations情况必须把

dateutil.gettype(); result = 1;

给删除掉,上述就是二者的区别

[    关键词:mock private的域或方法 invoke源类清单
    参考的例子,now方法是私有的/** * 演示mock private的域或方法 */public class dateutil { ......

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值