声明:
Author:赵志乾
Date:2017-7-30
Declaration:All Right Reserved!!!
从Mock对象的方法抛出异常
1、throwException动作
可以在will子句中使用throwException动作从Mock对象的方法中抛出异常,如下例所示:
allowing (bank).withdraw(with(any())); will(throwException(new WithdrawalLimitReachedException());
注:JMock会检测你试图抛出的代检异常是否与被调的Mock对象方法兼容。如果不兼容,JMock将会设定该测试失败并给出描述性的错误信息。如下例所示:
allowing (bank).withdraw(); will(throwException(new IllegalArgumentException("you cannot withdraw nothing!");
2、JMock允许Mock对象的方法抛出任何运行时异常和错误。
注:本博客中的实例代码均来自于JMock Cookbook。