• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.matchers;
6 
7 import static org.assertj.core.api.Assertions.assertThat;
8 import static org.junit.Assert.*;
9 import static org.mockito.Mockito.*;
10 
11 import java.util.ArrayList;
12 import java.util.List;
13 
14 import org.assertj.core.api.Assertions;
15 import org.junit.Test;
16 import org.mockito.ArgumentCaptor;
17 import org.mockito.exceptions.base.MockitoException;
18 import org.mockito.exceptions.verification.WantedButNotInvoked;
19 import org.mockitousage.IMethods;
20 import org.mockitoutil.TestBase;
21 
22 public class CapturingArgumentsTest extends TestBase {
23 
24     class Person {
25 
26         private final Integer age;
27 
Person(Integer age)28         public Person(Integer age) {
29             this.age = age;
30         }
31 
getAge()32         public int getAge() {
33             return age;
34         }
35     }
36 
37     class BulkEmailService {
38 
39         private EmailService service;
40 
BulkEmailService(EmailService service)41         public BulkEmailService(EmailService service) {
42             this.service = service;
43         }
44 
email(Integer... personId)45         public void email(Integer... personId) {
46             for (Integer i : personId) {
47                 Person person = new Person(i);
48                 service.sendEmailTo(person);
49             }
50         }
51     }
52 
53     interface EmailService {
sendEmailTo(Person person)54         boolean sendEmailTo(Person person);
55     }
56 
57     EmailService emailService = mock(EmailService.class);
58     BulkEmailService bulkEmailService = new BulkEmailService(emailService);
59     IMethods mock = mock(IMethods.class);
60 
61     @SuppressWarnings("deprecation")
62     @Test
should_allow_assertions_on_captured_argument()63     public void should_allow_assertions_on_captured_argument() {
64         // given
65         ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
66 
67         // when
68         bulkEmailService.email(12);
69 
70         // then
71         verify(emailService).sendEmailTo(argument.capture());
72         assertEquals(12, argument.getValue().getAge());
73     }
74 
75     @Test
should_allow_assertions_on_all_captured_arguments()76     public void should_allow_assertions_on_all_captured_arguments() {
77         // given
78         ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
79 
80         // when
81         bulkEmailService.email(11, 12);
82 
83         // then
84         verify(emailService, times(2)).sendEmailTo(argument.capture());
85         assertEquals(11, argument.getAllValues().get(0).getAge());
86         assertEquals(12, argument.getAllValues().get(1).getAge());
87     }
88 
89     @Test
should_allow_assertions_on_last_argument()90     public void should_allow_assertions_on_last_argument() {
91         // given
92         ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
93 
94         // when
95         bulkEmailService.email(11, 12, 13);
96 
97         // then
98         verify(emailService, times(3)).sendEmailTo(argument.capture());
99         assertEquals(13, argument.getValue().getAge());
100     }
101 
102     @Test
should_print_captor_matcher()103     public void should_print_captor_matcher() {
104         // given
105         ArgumentCaptor<Person> person = ArgumentCaptor.forClass(Person.class);
106 
107         try {
108             // when
109             verify(emailService).sendEmailTo(person.capture());
110             fail();
111         } catch (WantedButNotInvoked e) {
112             // then
113             assertThat(e).hasMessageContaining("<Capturing argument>");
114         }
115     }
116 
117     @Test
should_allow_assertions_on_captured_null()118     public void should_allow_assertions_on_captured_null() {
119         // given
120         ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
121 
122         // when
123         emailService.sendEmailTo(null);
124 
125         // then
126         verify(emailService).sendEmailTo(argument.capture());
127         assertEquals(null, argument.getValue());
128     }
129 
130     @Test
should_allow_construction_of_captor_for_parameterized_type_in_a_convenient_way()131     public void should_allow_construction_of_captor_for_parameterized_type_in_a_convenient_way() {
132         // the test passes if this expression compiles
133         @SuppressWarnings("unchecked")
134         ArgumentCaptor<List<Person>> argument = ArgumentCaptor.forClass(List.class);
135         assertNotNull(argument);
136     }
137 
138     @Test
should_allow_construction_of_captor_for_a_more_specific_type()139     public void should_allow_construction_of_captor_for_a_more_specific_type() {
140         // the test passes if this expression compiles
141         ArgumentCaptor<List<?>> argument = ArgumentCaptor.forClass(ArrayList.class);
142         assertNotNull(argument);
143     }
144 
145     @Test
should_allow_capturing_for_stubbing()146     public void should_allow_capturing_for_stubbing() {
147         // given
148         ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
149         when(emailService.sendEmailTo(argument.capture())).thenReturn(false);
150 
151         // when
152         emailService.sendEmailTo(new Person(10));
153 
154         // then
155         assertEquals(10, argument.getValue().getAge());
156     }
157 
158     @Test
should_capture_when_stubbing_only_when_entire_invocation_matches()159     public void should_capture_when_stubbing_only_when_entire_invocation_matches() {
160         // given
161         ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
162         when(mock.simpleMethod(argument.capture(), eq(2))).thenReturn("blah");
163 
164         // when
165         mock.simpleMethod("foo", 200);
166         mock.simpleMethod("bar", 2);
167 
168         // then
169         Assertions.assertThat(argument.getAllValues()).containsOnly("bar");
170     }
171 
172     @Test
should_say_something_smart_when_misused()173     public void should_say_something_smart_when_misused() {
174         ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
175         try {
176             argument.getValue();
177             fail();
178         } catch (MockitoException expected) {
179         }
180     }
181 
182     @Test
should_capture_when_full_arg_list_matches()183     public void should_capture_when_full_arg_list_matches() throws Exception {
184         // given
185         ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
186 
187         // when
188         mock.simpleMethod("foo", 1);
189         mock.simpleMethod("bar", 2);
190 
191         // then
192         verify(mock).simpleMethod(captor.capture(), eq(1));
193         assertEquals(1, captor.getAllValues().size());
194         assertEquals("foo", captor.getValue());
195     }
196 
197     @Test
should_capture_int_by_creating_captor_with_primitive_wrapper()198     public void should_capture_int_by_creating_captor_with_primitive_wrapper() {
199         // given
200         ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
201 
202         // when
203         mock.intArgumentMethod(10);
204 
205         // then
206         verify(mock).intArgumentMethod(argument.capture());
207         assertEquals(10, (int) argument.getValue());
208     }
209 
210     @Test
should_capture_int_by_creating_captor_with_primitive()211     public void should_capture_int_by_creating_captor_with_primitive() throws Exception {
212         // given
213         ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(int.class);
214 
215         // when
216         mock.intArgumentMethod(10);
217 
218         // then
219         verify(mock).intArgumentMethod(argument.capture());
220         assertEquals(10, (int) argument.getValue());
221     }
222 
223     @Test
should_capture_byte_vararg_by_creating_captor_with_primitive()224     public void should_capture_byte_vararg_by_creating_captor_with_primitive() throws Exception {
225         // given
226         ArgumentCaptor<Byte> argumentCaptor = ArgumentCaptor.forClass(byte.class);
227 
228         // when
229         mock.varargsbyte((byte) 1, (byte) 2);
230 
231         // then
232         verify(mock).varargsbyte(argumentCaptor.capture());
233         assertEquals((byte) 2, (byte) argumentCaptor.getValue());
234         Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly((byte) 1, (byte) 2);
235     }
236 
237     @Test
should_capture_byte_vararg_by_creating_captor_with_primitive_wrapper()238     public void should_capture_byte_vararg_by_creating_captor_with_primitive_wrapper()
239             throws Exception {
240         // given
241         ArgumentCaptor<Byte> argumentCaptor = ArgumentCaptor.forClass(Byte.class);
242 
243         // when
244         mock.varargsbyte((byte) 1, (byte) 2);
245 
246         // then
247         verify(mock).varargsbyte(argumentCaptor.capture());
248         assertEquals((byte) 2, (byte) argumentCaptor.getValue());
249         Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly((byte) 1, (byte) 2);
250     }
251 
252     @Test
should_capture_vararg()253     public void should_capture_vararg() throws Exception {
254         // given
255         ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
256 
257         // when
258         mock.mixedVarargs(42, "a", "b", "c");
259 
260         // then
261         verify(mock).mixedVarargs(any(), argumentCaptor.capture());
262         Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly("a", "b", "c");
263     }
264 
265     @Test
should_capture_all_vararg()266     public void should_capture_all_vararg() throws Exception {
267         // given
268         ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
269 
270         // when
271         mock.mixedVarargs(42, "a", "b", "c");
272         mock.mixedVarargs(42, "again ?!");
273 
274         // then
275         verify(mock, times(2)).mixedVarargs(any(), argumentCaptor.capture());
276 
277         Assertions.assertThat(argumentCaptor.getAllValues())
278                 .containsExactly("a", "b", "c", "again ?!");
279     }
280 
281     @Test
should_capture_one_arg_even_when_using_vararg_captor_on_nonvararg_method()282     public void should_capture_one_arg_even_when_using_vararg_captor_on_nonvararg_method()
283             throws Exception {
284         // given
285         ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
286 
287         // when
288         mock.simpleMethod("a", 2);
289 
290         // then
291         verify(mock).simpleMethod(argumentCaptor.capture(), eq(2));
292         Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly("a");
293     }
294 
295     @Test
captures_correctly_when_captor_used_multiple_times()296     public void captures_correctly_when_captor_used_multiple_times() throws Exception {
297         // given
298         ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
299 
300         // when
301         mock.mixedVarargs(42, "a", "b", "c");
302 
303         // then
304         // this is only for backwards compatibility. It does not make sense in real to do so.
305         verify(mock)
306                 .mixedVarargs(
307                         any(),
308                         argumentCaptor.capture(),
309                         argumentCaptor.capture(),
310                         argumentCaptor.capture());
311         Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly("a", "b", "c");
312     }
313 
314     @Test
captures_correctly_when_captor_used_on_pure_vararg_method()315     public void captures_correctly_when_captor_used_on_pure_vararg_method() throws Exception {
316         // given
317         ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
318 
319         // when
320         mock.varargs(42, "capturedValue");
321 
322         // then
323         verify(mock).varargs(eq(42), argumentCaptor.capture());
324         Assertions.assertThat(argumentCaptor.getValue()).contains("capturedValue");
325     }
326 }
327