(3)jmock测试入门

Returning Values from Mocked Methods

Unless you explicitly specify otherwise, jMock will return an appropriate value from any methods that do not have a void return type. In most tests you will need to explicitly define the value returned from a mocked invocation.

The Simple Case

You can return values from mocked methods by using the returnValue action within the "will" clause of an expectation.

oneOf (calculator).add(2, 2); will(returnValue(5));

jMock will fail the test if you try to return a value of the wrong type.

Returning Iterators over Collections

The returnIterator action returns an iterator over a collection.

final List<Employee> employees = new ArrayList<Employee>();
employees.add(alice);
employees.add(bob);

context.checking(new Expectations() {{
    oneOf (department).employees(); will(returnIterator(employees));
}});

A convenient overload of the returnIterator method lets you specify the elements inline:

context.checking(new Expectations() {{
    oneOf (department).employees(); will(returnIterator(alice, bob));
}});

Note the difference between using returnIterator and using returnValue to return an iterator. Using returnValue will return the same iterator each time: once all the iterator's elements have been consumed, further calls will return the same, exhausted iterator. The returnIterator action returns a new iterator each time it is invoked.

Returning Different Values on Consecutive Calls

There are two ways to return different values on different calls. The first is to define multiple expectations and return a different value from each:

oneOf (anObject).doSomething(); will(returnValue(10));
oneOf (anObject).doSomething(); will(returnValue(20));
oneOf (anObject).doSomething(); will(returnValue(30));

The first invocation of doSomething will return 10, the second 20 and the third 30.

However, this duplicates the definition of the expected invocation and so increases maintenance effort. A better approach is to use the onConsecutiveCalls action to return a different value (or perform a different action) on different invocations.

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

The expectation is expected at least 1 time, rather than 3 times, to make it easier to add more results later if we want to.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值