1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
| package com.ailk.ecs.ci;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.InjectMocks;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockito.verification.Timeout;
public class MockitoTest {
//1
//验证模拟的行为
//一旦创建了模拟对象,Mockito就会记录下此模拟对象的所有行为
@Test public void test1() {
//创建模拟的对象
List mockedList = mock(List.class);
//使用模拟的对象
mockedList.add("one");
mockedList.clear();
//验证模拟的对象是否被调用过
verify(mockedList).add("one");
verify(mockedList).clear();
}
//2
//如何进行打桩
@Test public void test2() {
//模拟的classes不一定是接口
LinkedList mockedList = mock(LinkedList.class);
//打桩!
when(mockedList.get(0)).thenReturn("first");
when(mockedList.get(1)).thenReturn(new RuntimeException());
//下面的执行结果会打印"first"
System.out.println(mockedList.get(0));
//下面的执行结果会跑出 runtime exception
System.out.println(mockedList.get(1));
//下面的运行结果会打印 "null" 因为 get(999) 没有进行过打桩
System.out.println(mockedList.get(999));
}
//3
//参数匹配器(matchers)
@Test public void test3() {
//用anyInt()参数匹配器进行打桩
LinkedList mockedList = mock(LinkedList.class);
//如果您使用的参数匹配,所有参数都必须提供匹配!!!!
when(mockedList.get(anyInt())).thenReturn("element");
//用hamcrest进行打桩(当我们执行 isValid() 方法时,会返回一个自己定义 hamcrest匹配器)
//when(mockedList.contains(argThat(isValid()))).thenReturn("element");
//下面的执行结果会打印"element"
System.out.println(mockedList.get(999));
//你同样可以以下面形式,进行验证
verify(mockedList).get(anyInt());
//下面的写法是错误的
//verify(mock).someMethod(anyInt(), anyString(), eq("third argument"));
//verify(mock).someMethod(anyInt(), anyString(), "third argument");
}
//4
//验证确切的请求次数 at least x / never
@Test public void test4() {
LinkedList mockedList = mock(LinkedList.class);
mockedList.add("once");
mockedList.add("twice");
mockedList.add("twice");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
//下面两种方法是等价的
//times(1)是默认的,因此可以省略
verify(mockedList).add("once");
verify(mockedList, times(1)).add("once");
//其他一些实用的方法
verify(mockedList, never()).add("never happened");
verify(mockedList, atLeastOnce()).add("three times");
verify(mockedList, atLeast(2)).add("five times");
verify(mockedList, atMost(5)).add("three times");
}
//5
//void 方法异常的打桩
@Test public void test5() {
LinkedList mockedList = mock(LinkedList.class);
doThrow(new RuntimeException()).when(mockedList).clear();
//下面会抛出 RuntimeException
mockedList.clear();
}
//6
//验证调用顺序
@Test public void test6() {
//A. 单一的方法是否按照特定的顺序执行
List singleMock = mock(List.class);
singleMock.add("was added first");
singleMock.add("was added second");
//为 single mock 创建一个 inOrder 验证器
InOrder inOrder = inOrder(singleMock);
//开始对执行的顺序进行校验
inOrder.verify(singleMock).add("was added first");
inOrder.verify(singleMock).add("was added second");
//B. 多个模拟的对象的方法是否按照顺序执行
List firstMock = mock(List.class);
List secondMock = mock(List.class);
firstMock.add("was called first");
secondMock.add("was called second");
InOrder inOrder2 = inOrder(firstMock, secondMock);
inOrder2.verify(firstMock).add("was called first");
inOrder2.verify(secondMock).add("was called second");
//C. 当然 A + B 的形式也可以混合使用,这里就略掉了
}
//7
//确定模拟对象之间没有相互调用
@Test public void test7() {
List mockOne = mock(List.class);
List mockTwo = mock(List.class);
List mockThree = mock(List.class);
mockOne.add("one");
verify(mockOne).add("one");
verify(mockOne, never()).add("two");
verifyZeroInteractions(mockTwo, mockThree);
//如果这样 mockTwo.add(mockThree) 执行上面的验证时,就会报错了
}
//8
//找到冗余的调用
@Test public void test8() {
LinkedList mockedList = mock(LinkedList.class);
mockedList.add("one");
mockedList.add("two");
verify(mockedList).add("one");
//下面的执行会失败
verifyNoMoreInteractions(mockedList);
}
//9
//利用 @Mock 注解进行创建模拟对象
//Important! This needs to be somewhere in the base class or a test runner:
public class Aclass {
public Object someMethod(String arg) {
return null;
}
}
//10
//连续调用桩
@Test public void test10() {
Aclass mock = mock(Aclass.class);
when(mock.someMethod("some arg"))
.thenThrow(new RuntimeException())
.thenReturn("foo");
try{
//第一次调用,会抛出 runtime exception
mock.someMethod("some arg");
} catch (RuntimeException e) {
System.out.println("runtime exception");
}
//第二次调用,会打印"foo"
System.out.println(mock.someMethod("some arg"));
//继续调用,会一直打印"foo"
System.out.println(mock.someMethod("some arg"));
//另一种简略的写法
when(mock.someMethod("some arg"))
.thenReturn("one", "two", "three");
System.out.println("1:" + mock.someMethod("some arg"));
System.out.println("2:" + mock.someMethod("some arg"));
System.out.println("3:" + mock.someMethod("some arg"));
}
//11
//对回调方法进行打桩
@Test public void test11(){
Aclass mock = mock(Aclass.class);
when(mock.someMethod(anyString())).thenAnswer(new Answer(){
public Object answer(InvocationOnMock invocation) {
Object[] args = invocation.getArguments();
// Object mock = invocation.getMock();
return "called with arguments:" + args[0];
}
});
Object a = mock.someMethod("foo");
//下面将会打印"called with arguments: foo"
System.out.println(mock.someMethod("foo"));
}
//12
//doThrow()|doAnswer()|doNothing()|doReturn() 主要是用于 void 类型打桩
@Test public void test12() {
LinkedList mockedList = mock(LinkedList.class);
doThrow(new RuntimeException()).when(mockedList).clear();
//following throws RuntimeException:
mockedList.clear();
}
//13
//spying调用真正对象
@Test public void test13() {
List list = new LinkedList();
List spy = spy(list);
//您也可以选择一些方法的存根
when(spy.size()).thenReturn(100);
//调用spy的真实方法
spy.add("one");
spy.add("two");
//打印 "one" - list的第一个原素
System.out.println(spy.get(0));
//被打桩的size方法,会打印100
System.out.println(spy.size());
//您也可以进行验证
verify(spy).add("one");
verify(spy).add("two");
}
//14
//更改未进行打桩的默认返回值(Since 1.7)
//当如下初始化一个mock的时候,如果调用未被打桩,会返回默认值
//下面的两种调用,会返回空指针异常
@Test public void test14() {
// Foo mock = mock(Foo.class, Mockito.RETURNS_SMART_NULLS);
// Foo mockTwo = mock(Foo.class, new YourOwnAnswer());
// //calling unstubbed method here:
// Stuff stuff = mock.getStuff();
// //using object returned by unstubbed call:
// stuff.doSomething();
}
//15
//为进一步断言捕捉参数(Since 1.8.0)
@Test public void test15() {
// ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
// verify(mock).doSomething(argument.capture());
// assertEquals("John", argument.getValue().getName());
}
public class Foo {
public String someMethod(){
return "real Method!";
}
}
//16
//真正的局部模拟 (Since 1.8.0)
@Test public void test16() {
//方法1.spy
List list = spy(new LinkedList());
//方法2.可以选择性的使用局部模拟
Foo mock = mock(Foo.class);
when(mock.someMethod()).thenCallRealMethod();
System.out.println(mock.someMethod());
}
//17
//模拟的重置(Since 1.8.0)
@Test public void test17() {
List mock = mock(List.class);
when(mock.size()).thenReturn(10);
System.out.println(mock.size());
reset(mock);
System.out.println(mock.size());
}
//18
//19
//行为驱动开发(behavior driven development - BDD)
@Test public void shouldBuyBread19() {
// Seller seller = mock(Seller.class);
// Shop shop = new Shop(seller);
//
// //given
// given(seller.askForBread()).willReturn(new Bread());
// //when
// Goods goods = shop.buyBread();
// //then
// assertThat(goods, containBread());
}
//20
//序列化的模拟 (Since 1.8.1)
@Test public void test20() {
//1
List serializableMock = mock(List.class, withSettings().serializable());
//2
List<Object> list = new ArrayList<Object>();
List<Object> spy = mock(ArrayList.class, withSettings()
.spiedInstance(list)
.defaultAnswer(CALLS_REAL_METHODS)
.serializable());
}
//21
//注解: @Captor, @Spy, @InjectMocks (Since 1.8.3)
//22
//验证超时 (Since 1.8.5)
@Test public void test22() {
Foo mock = mock(Foo.class);
//passes when someMethod() is called within given time span
verify(mock, timeout(100)).someMethod();
//above is an alias to:
verify(mock, timeout(100).times(1)).someMethod();
//passes when someMethod() is called *exactly* 2 times within given time span
verify(mock, timeout(100).times(2)).someMethod();
//passes when someMethod() is called *at lest* 2 times within given time span
verify(mock, timeout(100).atLeast(2)).someMethod();
//verifies someMethod() within given time span using given verification mode
//useful only if you have your own custom verification modes.
// verify(mock, new Timeout(100, yourOwnVerificationMode)).someMethod();
}
//23
//@Spies, @InjectMocks 注释更优雅的(相对于1.8.3)注入方式 (Since 1.9)
@Test public void test23() {
// //代替:
// @Spy BeerDrinker drinker = new BeerDrinker();
// //你可以写成:
// @Spy BeerDrinker drinker;
//
// //同样适用于 @InjectMocks 注释:
// @InjectMocks LocalPub;
}
//24
//用链式方法进行打桩 (Since 1.9)
@Test public void test24() {
// Car boringStubbedCar = when(mock(Car.class).shiftGear()).thenThrow(EngineNotStarted.class).getMock();
}
//25
//验证被忽视的桩 (Since 1.9)
@Test public void test25() {
// verify(mock).foo();
// verify(mockTwo).bar();
//
// //ignores all stubbed methods:
// verifyNoMoreInvocations(ignoreStubs(mock, mockTwo));
//
// //creates InOrder that will ignore stubbed
// InOrder inOrder = inOrder(ignoreStubs(mock, mockTwo));
// inOrder.verify(mock).foo();
// inOrder.verify(mockTwo).bar();
// inOrder.verifyNoMoreInteractions();
}
}
|