forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvalidUsageTest.java
226 lines (198 loc) · 8.43 KB
/
InvalidUsageTest.java
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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.misuse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import org.junit.After;
import org.junit.Assume;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.misusing.MissingMethodInvocationException;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.plugins.InlineMockMaker;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
public class InvalidUsageTest extends TestBase {
@Mock private IMethods mock;
@Mock private IMethods mockTwo;
@After
public void resetState() {
super.resetState();
}
@Test
public void shouldRequireArgumentsWhenVerifyingNoMoreInteractions() {
assertThatThrownBy(
() -> {
verifyNoMoreInteractions();
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"Method requires argument(s)!",
"Pass mocks that should be verified, e.g:",
" verifyNoMoreInteractions(mockOne, mockTwo);",
" verifyNoInteractions(mockOne, mockTwo);");
}
@Test
public void shouldRequireArgumentsWhenVerifyingNoInteractions() {
assertThatThrownBy(
() -> {
verifyNoInteractions();
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"Method requires argument(s)!",
"Pass mocks that should be verified, e.g:",
" verifyNoMoreInteractions(mockOne, mockTwo);",
" verifyNoInteractions(mockOne, mockTwo);");
}
@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test
public void shouldNotCreateInOrderObjectWithoutMocks() {
assertThatThrownBy(
() -> {
inOrder();
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"Method requires argument(s)!",
"Pass mocks that require verification in order.",
"For example:",
" InOrder inOrder = inOrder(mockOne, mockTwo);");
}
@Test
public void shouldNotAllowVerifyingInOrderUnfamiliarMocks() {
InOrder inOrder = inOrder(mock);
assertThatThrownBy(
() -> {
inOrder.verify(mockTwo).simpleMethod();
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"InOrder can only verify mocks that were passed in during creation of InOrder.",
"For example:",
" InOrder inOrder = inOrder(mockOne);",
" inOrder.verify(mockOne).doStuff();");
}
@Test
public void shouldReportMissingMethodInvocationWhenStubbing() {
when(mock.simpleMethod())
.thenReturn("this stubbing is required to make sure Stubbable is pulled");
assertThatThrownBy(
() -> {
when("".toString()).thenReturn("x");
})
.isInstanceOf(MissingMethodInvocationException.class)
.hasMessageContainingAll(
"when() requires an argument which has to be 'a method call on a mock'.",
"For example:",
" when(mock.getArticles()).thenReturn(articles);",
"Also, this error might show up because:",
"1. you stub either of: final/private/equals()/hashCode() methods.",
" Those methods *cannot* be stubbed/verified.",
" Mocking methods declared on non-public parent classes is not supported.",
"2. inside when() you don't call method on mock but on some other object.");
}
@Test
public void shouldNotAllowSettingInvalidCheckedException() {
assertThatThrownBy(
() -> {
when(mock.simpleMethod()).thenThrow(new Exception());
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"Checked exception is invalid for this method!",
"Invalid: java.lang.Exception");
}
@Test
public void shouldNotAllowSettingNullThrowable() {
assertThatThrownBy(
() -> {
when(mock.simpleMethod()).thenThrow(new Throwable[] {null});
})
.isInstanceOf(MockitoException.class)
.hasMessageContaining("Cannot stub with null throwable!");
}
@SuppressWarnings("all")
@Test
public void shouldNotAllowSettingNullThrowableVararg() throws Exception {
assertThatThrownBy(
() -> {
when(mock.simpleMethod()).thenThrow((Throwable) null);
})
.isInstanceOf(MockitoException.class)
.hasMessageContaining("Cannot stub with null throwable!");
}
@Test
public void shouldNotAllowSettingNullConsecutiveThrowable() {
assertThatThrownBy(
() -> {
when(mock.simpleMethod()).thenThrow(new RuntimeException(), null);
})
.isInstanceOf(MockitoException.class)
.hasMessageContaining("Cannot stub with null throwable!");
}
final class FinalClass {}
@Test
public void shouldNotAllowMockingFinalClassesIfDisabled() {
Assume.assumeThat(Plugins.getMockMaker(), not(instanceOf(InlineMockMaker.class)));
assertThatThrownBy(
() -> {
mock(FinalClass.class);
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"Cannot mock/spy class org.mockitousage.misuse.InvalidUsageTest$FinalClass",
"Mockito cannot mock/spy because :",
" - final class");
}
@Test
public void shouldAllowMockingFinalClassesIfEnabled() {
Assume.assumeThat(Plugins.getMockMaker(), instanceOf(InlineMockMaker.class));
assertThat(mock(FinalClass.class)).isInstanceOf(FinalClass.class);
}
@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test
public void shouldNotAllowMockingPrimitives() {
assertThatThrownBy(
() -> {
mock(Integer.TYPE);
})
.isInstanceOf(MockitoException.class)
.hasMessageContainingAll(
"Cannot mock/spy int",
"Mockito cannot mock/spy because :",
" - primitive type");
}
interface ObjectLikeInterface {
boolean equals(Object o);
String toString();
int hashCode();
}
@Test
public void shouldNotMockObjectMethodsOnInterfaceVerifyNoInteractions() {
ObjectLikeInterface inter = mock(ObjectLikeInterface.class);
Object ignored = inter.equals(null);
ignored = inter.toString();
ignored = inter.hashCode();
verifyNoInteractions(inter);
}
@Test
public void shouldNotMockObjectMethodsOnClassVerifyNoInteractions() {
Object clazz = mock(ObjectLikeInterface.class);
Object ignored = clazz.equals(null);
ignored = clazz.toString();
ignored = clazz.hashCode();
verifyNoInteractions(clazz);
}
}