Jmock使用FAQ--转载

本文介绍了JMock单元测试工具的使用技巧,包括如何校验expectations规则、设定方法调用次数及顺序、指定mock方法抛出异常等。还详细解释了one与oneOf的区别、如何设定方法连续调用返回不同值及如何利用状态机state进行更复杂的测试场景设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 如何校验expectations中的规则?

使用JMock时,一般会通过如下代码指定expectations。

为了校验expectations中的规则是否都满足,可以在测试完成后通过增加context.assertIsSatisfied()方法来验证expectations是否满足。

//test assertions and test logic code.   context.assertIsSatisfied();

若校验不成果,测试将失败。

2.one和oneOf的区别?

one(mockObject) 以后会depreacted; 即one方法即将过时。
所以因该使用oneOf(mockObject) 来代替.

鉴于我们目前使用的是JMock2.4版,所以大家还是直接使用one就ok了。oneOf是2.51版中的内容。

3.如何设定使用expectation的语法来指定同一个方法连续调用时返回不同的值?

有两种方法,第一种就是直接通过多次调用 will(returnValue(X))来指定。如

第一次调用时会返回10,第二次会返回20,第三次会返回30.

然而第一一种方法会增加维护成本,且缺乏可控性。JMock提供了第二种方法,即通过onConsecutiveCalls的action来实现返回不同的返回值。如:

atLeast(1).of (anObject).doSomething();  will(onConsecutiveCalls(      returnValue(10),      returnValue(20),      returnValue(30)));

这里atLeast(1)表明doSomething方法将至少被调用一次,但不超过3次。且调用的返回值分别是10、20、30.

4.如何指定mock的方法抛出异常?

在will方法中直接使用throwException的action。参考如下语法:

oneOf (anObject).doSomething();will(throwException(new UserDefinedException());
5.如何指定被mock方法的调用次数?

参考Jmock的文档,调用次数可以通过 oneOf,exactly(times).of, atLeast(times).of,atMost(times).of, between(min, max).of, allowing, ignoring, never等来指定。具体含义见下表。

JMockdefines the following cardinalities:
oneThe invocation is expected once and onceonly.
exactly(times).ofThe invocation is expected exactly ntimes. Note: one is a convenient shorthand for exactly(1).
atLeast(times).ofThe invocation is expected at least ntimes.
atMost(times).ofThe invocation is expected at most ntimes.
between(min, max).ofThe invocation is expected at least mintimes and at most max times.
allowingThe invocation is allowed any number oftimes but does not have to happen.
ignoringThe same as allowing. Allowing or ignoringshould be chosen to make the test code clearly express intent.
neverThe invocation is not expected at all.This is used to make tests more explicit and so easier tounderstand.

需要强调的是allowing和ignoring的规则不会破坏context.assertIsSatisfied()的校验。

6.什么时候使用expect,还有allowing呢?

根据JMock的cookbook。如果方法的调用是allowing的,哪么在测试时,这个方法允许被调用,并且如果方法未被调用测试仍然会通过。反之,如果一个方法是expected的(即通过one,oneOf,exactly(times).of,atLeast(times).of, atMost(times).of, between(min, max).of等指定次数的),则该方法必须满足次数的要求,否则测试将失败。附上Cookbook上的内容参考。

Expecting vs.Allowing

The most important distinction that cardinalities express isbetween expecting and allowing an invocation.

If an invocation is allowed, it can occur during a test run, butthe test still passes if the invocation does not occur. If aninvocation is expected, on the other hand, it must occur during atest run; if it does not occur, the test fails. When definingexpectations you must choose whether to expect or allow eachinvocation. In general, we find that tests are kept flexible if youallow queries and expect commands. A query is a method with no sideeffects that does nothing but query the state of an object and acommand is a method with side effects that may, or may not, returna result. Of course, this does not hold all the time but it’s auseful rule of thumb to follow if there are no other constraints onthe number of times you expect a method to be called in yourtest.

7.如何指定方法被调用的次序(Sequence)呢?

当需要指定方法调用次序时,可以首先声明一个JMock 的sequence对象。

final Sequence sequenceName= context.sequence("sequenceName");

然后通过增加inSequence(sequenceName)的语法来指定不同方法的调用次序:

oneOf (turtle).forward(10); inSequence(sequenceName);oneOf (turtle).turn(45); inSequence(sequenceName);atMost(5).of (turtle).forward(10); inSequence(sequenceName);oneOf (turtle).backward(20); inSequence(sequenceName);

这样方法被调用的顺序必须按照forwar、turn、forward、backward的顺序来进行。此外如果Sequence中的方法的expectations是allowing的,则将被Sequence忽略。

8如何使用JMock的状态机state。

状态机state和Sequence的使用方法类似。首先声明state。

//构建状态机,并确定初始状态为"initialState"。 final States stateMachineName = context.states("stateMachineName ").startsAs("initialState");
然后通过如下语法来使用

The following clauses constrain invocations to occur withinspecific states and define how an invocation will change thecurrent state of a state machine.

when(state-machine.is(”state-name”));Constrains the last expectation to occuronly when the state machine is in the named state.
when(state-machine.isNot(”state-name”));Constrains the last expectation to occuronly when the state machine is not in the named state.
then(state-machine.is(”state-name”));Changes the state of state-machine to thenamed state when the invocation occurs.

转载:

http://blog.sina.com.cn/s/blog_6b3380380100t4fz.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值