(3)jmock测试入门
来源:互联网 发布:iphone一键越狱软件 编辑:程序博客网 时间:2024/06/06 00:16
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.
- (3)jmock测试入门
- (1)jmock测试入门
- (2)jmock测试入门
- 菜鸟学习Jmock测试-入门(一)
- 菜鸟学习Jmock测试-入门(二)
- 菜鸟学习Jmock测试-入门(三)
- JMock测试入门
- JMock测试入门
- [JMock] JMock快速入门
- Jmock测试
- JMock入门
- JMock使用入门
- Junit - JMock 入门
- 测试工具(三)——jMock 1.2
- 菜鸟学习Jmock测试-全解(四)
- java 测试框架 JMOCK使用
- JMock
- Jmock
- 网页计算器(javascript脚本)
- CF 240 D. Mashmokh and ACM
- JNDI 是什么
- 用DetachedCriteria查询
- php文件管理工具——RESPONSIVE filemanager
- (3)jmock测试入门
- DES算法实现(C++版)
- jq select操作
- 将visual studio 中的解决方案整体移植给别人
- typedef的用途
- Java Class类学习
- 漫谈C++内存分配失败
- Android APK 反编译图文详解
- Populating Next Right Pointers in Each Node I & II