Skip to content

Commit 21a9908

Browse files
committed
example: mockito
1 parent 7613001 commit 21a9908

1 file changed

Lines changed: 208 additions & 0 deletions

File tree

  • codes/javalib/test/src/test/java/io/github/dunwu/javalib/mockito
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package io.github.dunwu.javalib.mockito;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.mockito.InOrder;
6+
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
10+
import static org.mockito.Mockito.*;
11+
12+
class MockitoTest {
13+
// 模拟 LinkedList 的一个对象
14+
LinkedList mockedList = mock(LinkedList.class);
15+
16+
@BeforeEach
17+
void beforeEach() {
18+
mockedList.clear();
19+
}
20+
21+
@Test
22+
void test() {
23+
//using mock object - it does not throw any "unexpected interaction" exception
24+
mockedList.add("one");
25+
// selective, explicit, highly readable verification
26+
verify(mockedList).add("one");
27+
}
28+
29+
/**
30+
* 模拟对象
31+
*/
32+
@Test
33+
void test01() {
34+
// 此时调用get方法,会返回null,因为还没有对方法调用的返回值做模拟
35+
System.out.println(mockedList.get(0));
36+
}
37+
38+
/**
39+
* 模拟方法调用的返回值
40+
*/
41+
@Test
42+
void test02() {
43+
// 模拟获取第一个元素时,返回字符串first。给特定的方法调用返回固定值在官方说法中称为stub。
44+
when(mockedList.get(0)).thenReturn("first");
45+
// 此时打印输出first
46+
System.out.println(mockedList.get(0));
47+
}
48+
49+
/**
50+
* 模拟方法调用抛出异常
51+
*/
52+
@Test
53+
void test03() {
54+
// 模拟获取第二个元素时,抛出RuntimeException
55+
when(mockedList.get(1)).thenThrow(new RuntimeException());
56+
// 此时将会抛出RuntimeException
57+
System.out.println(mockedList.get(1));
58+
}
59+
60+
/**
61+
* 模拟方法调用抛出异常2
62+
*/
63+
@Test
64+
void test04() {
65+
doThrow(new RuntimeException("clear exception")).when(mockedList).clear();
66+
mockedList.clear();
67+
}
68+
69+
/**
70+
* 模拟调用方法时的参数匹配
71+
*/
72+
@Test
73+
void test05() {
74+
// anyInt()匹配任何int参数,这意味着参数为任意值,其返回值均是element
75+
when(mockedList.get(anyInt())).thenReturn("element");
76+
// 此时打印是element
77+
System.out.println(mockedList.get(999));
78+
}
79+
80+
/**
81+
* 模拟方法调用次数
82+
*/
83+
@Test
84+
void test06() {
85+
// 调用add一次
86+
mockedList.add("once");
87+
// 下面两个写法验证效果一样,均验证add方法是否被调用了一次
88+
verify(mockedList).add("once");
89+
verify(mockedList, times(1)).add("once");
90+
}
91+
92+
/**
93+
* 校验行为
94+
*/
95+
@Test
96+
void test07() {
97+
// using mock object
98+
mockedList.add("one");
99+
mockedList.clear();
100+
//verification
101+
verify(mockedList).add("one");
102+
verify(mockedList).clear();
103+
}
104+
105+
/**
106+
* 模拟方法调用(Stubbing)
107+
*/
108+
@Test
109+
void test08() {
110+
//stubbing
111+
when(mockedList.get(0)).thenReturn("first");
112+
when(mockedList.get(1)).thenThrow(new RuntimeException());
113+
//following prints "first"
114+
System.out.println(mockedList.get(0));
115+
//following throws runtime exception
116+
System.out.println(mockedList.get(1));
117+
//following prints "null" because get(999) was not stubbed
118+
System.out.println(mockedList.get(999));
119+
120+
verify(mockedList).get(0);
121+
}
122+
123+
/**
124+
* 校验方法调用次数
125+
*/
126+
@Test
127+
void test09() {
128+
//using mock
129+
mockedList.add("once");
130+
131+
mockedList.add("twice");
132+
mockedList.add("twice");
133+
134+
mockedList.add("three times");
135+
mockedList.add("three times");
136+
mockedList.add("three times");
137+
//following two verifications work exactly the same - times(1) is used by default
138+
verify(mockedList).add("once");
139+
verify(mockedList, times(1)).add("once");
140+
//exact number of invocations verification
141+
verify(mockedList, times(2)).add("twice");
142+
verify(mockedList, times(3)).add("three times");
143+
//verification using never(). never() is an alias to times(0)
144+
verify(mockedList, never()).add("never happened");
145+
//verification using atLeast()/atMost()
146+
verify(mockedList, atLeastOnce()).add("three times");
147+
verify(mockedList, atLeast(2)).add("five times");
148+
verify(mockedList, atMost(5)).add("three times");
149+
}
150+
151+
/**
152+
* 校验方法调用顺序
153+
*/
154+
@Test
155+
void test10() {
156+
// A. Single mock whose methods must be invoked in a particular order
157+
List singleMock = mock(List.class);
158+
//using a single mock
159+
singleMock.add("was added first");
160+
singleMock.add("was added second");
161+
//create an inOrder verifier for a single mock
162+
InOrder inOrder = inOrder(singleMock);
163+
//following will make sure that add is first called with "was added first, then with "was added second"
164+
inOrder.verify(singleMock).add("was added first");
165+
inOrder.verify(singleMock).add("was added second");
166+
167+
// B. Multiple mocks that must be used in a particular order
168+
List firstMock = mock(List.class);
169+
List secondMock = mock(List.class);
170+
//using mocks
171+
firstMock.add("was called first");
172+
secondMock.add("was called second");
173+
//create inOrder object passing any mocks that need to be verified in order
174+
inOrder = inOrder(firstMock, secondMock);
175+
//following will make sure that firstMock was called before secondMock
176+
inOrder.verify(firstMock).add("was called first");
177+
inOrder.verify(secondMock).add("was called second");
178+
// Oh, and A + B can be mixed together at will
179+
}
180+
181+
/**
182+
* 校验方法是否从未调用
183+
*/
184+
@Test
185+
void test11() {
186+
List mockOne = mock(List.class);
187+
List mockTwo = mock(List.class);
188+
List mockThree = mock(List.class);
189+
//using mocks - only mockOne is interacted
190+
mockOne.add("one");
191+
//ordinary verification
192+
verify(mockOne).add("one");
193+
//verify that method was never called on a mock
194+
verify(mockOne, never()).add("two");
195+
//verify that other mocks were not interacted
196+
verifyZeroInteractions(mockTwo, mockThree);
197+
}
198+
199+
/**
200+
* 重置Mock
201+
*/
202+
void test12() {
203+
List mock = mock(List.class);
204+
when(mock.size()).thenReturn(10);
205+
mock.add(1);
206+
reset(mock);
207+
}
208+
}

0 commit comments

Comments
 (0)