对于mock对象上的mock方法的调用,easymock支持指定次数,默认为1,例如
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
这里没有显式的指定调用次数,因此效果等同于
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
同时easymock提供了其他的方法,用于指定具体调用次数或者放宽调用次数检验。
1. once()
如果明确调用次数为1,则可以使用这个方法显式指定,也可以省略,easymock默认为1。
2. atLeastOnce()
指定调用为1次或者多次,即 count >= 1.
3. anyTimes()
容许调用次数为任意次,即 count >= 0.
4. times(int count)
直接指定调用次数
5. times(int min, int max)
这个方法比较灵活,可以指定最小次数和最大次数。
其他的方法都可以视为这个方法的变体,比如
once()等价于times(1,1)
atLeastOnce()等价于times(1,Integer.MAX_VALUE)
anyTimes()等价于times(0,Integer.MAX_VALUE)
times(int count)等价于times(count,count)
实际使用中根据具体要求可以灵活选用上述方法来指定我们期望的调用数次。