Package: mockito / 2.23.0-1

15-backward-compatibility.patch Patch series | download
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
Description: Partially restore the backward compatibility with Mockito 1.x
Author: Emmanuel Bourg <ebourg@apache.org>
Forwarded: not-needed
--- a/src/main/java/org/mockito/Matchers.java
+++ b/src/main/java/org/mockito/Matchers.java
@@ -4,10 +4,157 @@
  */
 package org.mockito;
 
+import org.hamcrest.Matcher;
+import org.mockito.ArgumentMatcher;
+import org.mockito.internal.hamcrest.HamcrestArgumentMatcher;
+
+import static org.mockito.internal.hamcrest.MatcherGenericTypeExtractor.genericTypeOfMatcher;
+import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;
+import static org.mockito.internal.util.Primitives.defaultValue;
+
 /**
  * @deprecated Use {@link ArgumentMatchers}. This class is now deprecated in order to avoid a name clash with Hamcrest
  * <code>org.hamcrest.Matchers</code> class. This class will likely be removed in version 3.0.
  */
 @Deprecated
 public class Matchers extends ArgumentMatchers {
+
+    /**
+     * Allows matching arguments with hamcrest matchers.
+     * <p/>
+     * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>null</code> or default value for primitive (0, false, etc.)
+     */
+    @SuppressWarnings("unchecked")
+    @Deprecated
+    public static <T> T argThat(Matcher<T> matcher) {
+        reportMatcher(matcher);
+        return  (T) defaultValue(genericTypeOfMatcher(matcher.getClass()));
+    }
+
+    /**
+     * Enables integrating hamcrest matchers that match primitive <code>char</code> arguments.
+     * Note that {@link #argThat} will not work with primitive <code>char</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.
+     * <p/>
+     * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>0</code>.
+     */
+    @Deprecated
+    public static char charThat(Matcher<Character> matcher) {
+        reportMatcher(matcher);
+        return 0;
+    }
+
+    /**
+     * Enables integrating hamcrest matchers that match primitive <code>boolean</code> arguments.
+     * Note that {@link #argThat} will not work with primitive <code>boolean</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.
+     * <p/>
+     * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>false</code>.
+     */
+    @Deprecated
+    public static boolean booleanThat(Matcher<Boolean> matcher) {
+        reportMatcher(matcher);
+        return false;
+    }
+
+    /**
+     * Enables integrating hamcrest matchers that match primitive <code>byte</code> arguments.
+     * Note that {@link #argThat} will not work with primitive <code>byte</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.
+     * <p/>
+     * * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>0</code>.
+     */
+    @Deprecated
+    public static byte byteThat(Matcher<Byte> matcher) {
+        reportMatcher(matcher);
+        return 0;
+    }
+
+    /**
+     * Enables integrating hamcrest matchers that match primitive <code>short</code> arguments.
+     * Note that {@link #argThat} will not work with primitive <code>short</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.
+     * <p/>
+     * * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>0</code>.
+     */
+    @Deprecated
+    public static short shortThat(Matcher<Short> matcher) {
+        reportMatcher(matcher);
+        return 0;
+    }
+
+    /**
+     * Enables integrating hamcrest matchers that match primitive <code>int</code> arguments.
+     * Note that {@link #argThat} will not work with primitive <code>int</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.
+     * <p/>
+     * * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>0</code>.
+     */
+    @Deprecated
+    public static int intThat(Matcher<Integer> matcher) {
+        reportMatcher(matcher);
+        return 0;
+    }
+
+    /**
+     * Enables integrating hamcrest matchers that match primitive <code>long</code> arguments.
+     * Note that {@link #argThat} will not work with primitive <code>long</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.
+     * <p/>
+     * * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>0</code>.
+     */
+    @Deprecated
+    public static long longThat(Matcher<Long> matcher) {
+        reportMatcher(matcher);
+        return 0;
+    }
+
+    /**
+     * Enables integrating hamcrest matchers that match primitive <code>float</code> arguments.
+     * Note that {@link #argThat} will not work with primitive <code>float</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.
+     * <p/>
+     * * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>0</code>.
+     */
+    @Deprecated
+    public static float floatThat(Matcher<Float> matcher) {
+        reportMatcher(matcher);
+        return 0;
+    }
+
+    /**
+     * Enables integrating hamcrest matchers that match primitive <code>double</code> arguments.
+     * Note that {@link #argThat} will not work with primitive <code>double</code> matchers due to <code>NullPointerException</code> auto-unboxing caveat.
+     * <p/>
+     * * See examples in javadoc for {@link MockitoHamcrest} class
+     *
+     * @param matcher decides whether argument matches
+     * @return <code>0</code>.
+     */
+    @Deprecated
+    public static double doubleThat(Matcher<Double> matcher) {
+        reportMatcher(matcher);
+        return 0;
+    }
+
+    private static <T> void reportMatcher(Matcher<T> matcher) {
+        mockingProgress().getArgumentMatcherStorage().reportMatcher(new HamcrestArgumentMatcher<T>(matcher));
+    }
 }
--- /dev/null
+++ b/src/main/java/org/mockito/stubbing/answers/ReturnsElementsOf.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (c) 2007 Mockito contributors
+ * This program is made available under the terms of the MIT License.
+ */
+package org.mockito.stubbing.answers;
+
+import org.mockito.exceptions.base.MockitoException;
+import org.mockito.invocation.InvocationOnMock;
+import org.mockito.stubbing.Answer;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Returns elements of the collection. Keeps returning the last element forever.
+ * Might be useful on occasion when you have a collection of elements to return.
+ * <p>
+ * <pre class="code"><code class="java">
+ *   //this:
+ *   when(mock.foo()).thenReturn(1, 2, 3);
+ *   //is equivalent to:
+ *   when(mock.foo()).thenAnswer(new ReturnsElementsOf(Arrays.asList(1, 2, 3)));
+ * </code></pre>
+ * </p>
+ *
+ * <p>
+ * Also you might better want to use the static factory there
+ * {@link org.mockito.AdditionalAnswers#returnsElementsOf(java.util.Collection)}
+ * </p>
+ *
+ * @see org.mockito.AdditionalAnswers
+ */
+@Deprecated
+public class ReturnsElementsOf implements Answer<Object> {
+
+    private final LinkedList<Object> elements;
+
+    public ReturnsElementsOf(Collection<?> elements) {
+        if (elements == null) {
+            throw new MockitoException("ReturnsElementsOf does not accept null as constructor argument.\n" +
+            		"Please pass a collection instance");
+        }
+        this.elements = new LinkedList<Object>(elements);
+    }
+
+    public Object answer(InvocationOnMock invocation) throws Throwable {
+        if (elements.size() == 1)
+            return elements.get(0);
+        else 
+            return elements.poll();
+    }
+}
+
--- a/src/main/java/org/mockito/junit/MockitoJUnitRunner.java
+++ b/src/main/java/org/mockito/junit/MockitoJUnitRunner.java
@@ -151,7 +151,7 @@
 
     public MockitoJUnitRunner(Class<?> klass) throws InvocationTargetException {
         //by default, StrictRunner is used. We can change that potentially based on feedback from users
-        this(new StrictRunner(new RunnerFactory().createStrict(klass), klass));
+        this(new RunnerFactory().create(klass));
     }
 
     MockitoJUnitRunner(InternalRunner runner) throws InvocationTargetException {
--- /dev/null
+++ b/src/main/java/org/mockito/runners/MockitoJUnit44Runner.java
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2007 Mockito contributors
+ * This program is made available under the terms of the MIT License.
+ */
+
+package org.mockito.runners;
+
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+
+import java.lang.reflect.InvocationTargetException;
+
+/**
+ * <b>Deprecated: Simply use {@link MockitoJUnitRunner}</b>
+ * <p>
+ * Compatible only with <b>JUnit 4.4</b>, this runner adds following behavior:
+ * <ul>
+ *   <li>
+ *      Initializes mocks annotated with {@link Mock},
+ *      so that explicit usage of {@link MockitoAnnotations#initMocks(Object)} is not necessary. 
+ *      Mocks are initialized before each test method.
+ *   <li>
+ *      validates framework usage after each test method. See javadoc for {@link Mockito#validateMockitoUsage()}.
+ * </ul>
+ * 
+ * Runner is completely optional - there are other ways you can get &#064;Mock working, for example by writing a base class.
+ * Explicitly validating framework usage is also optional because it is triggered automatically by Mockito every time you use the framework.
+ * See javadoc for {@link Mockito#validateMockitoUsage()}.
+ * <p>
+ * Read more about &#064;Mock annotation in javadoc for {@link MockitoAnnotations}
+ * <p>
+ * Example:
+ * <pre class="code"><code class="java">
+ * &#064;RunWith(MockitoJUnitRunner.class)
+ * public class ExampleTest {
+ * 
+ *     &#064;Mock
+ *     private List list;
+ * 
+ *     &#064;Test
+ *     public void shouldDoSomething() {
+ *         list.add(100);
+ *     }
+ * }
+ * <p>
+ * 
+ * </code></pre>
+ */
+@Deprecated
+public class MockitoJUnit44Runner extends MockitoJUnitRunner {
+
+    public MockitoJUnit44Runner(Class<?> klass) throws InvocationTargetException {
+        super(klass);
+    }
+}
\ No newline at end of file