• 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.stubbing;
6 
7 import static org.assertj.core.api.Assertions.assertThat;
8 import static org.mockito.AdditionalAnswers.answer;
9 import static org.mockito.AdditionalAnswers.answerVoid;
10 import static org.mockito.AdditionalAnswers.returnsArgAt;
11 import static org.mockito.AdditionalAnswers.returnsFirstArg;
12 import static org.mockito.AdditionalAnswers.returnsLastArg;
13 import static org.mockito.AdditionalAnswers.returnsSecondArg;
14 import static org.mockito.BDDMockito.any;
15 import static org.mockito.BDDMockito.anyInt;
16 import static org.mockito.BDDMockito.anyObject;
17 import static org.mockito.BDDMockito.anyString;
18 import static org.mockito.BDDMockito.anyVararg;
19 import static org.mockito.BDDMockito.eq;
20 import static org.mockito.BDDMockito.given;
21 import static org.mockito.BDDMockito.mock;
22 import static org.mockito.BDDMockito.times;
23 import static org.mockito.BDDMockito.verify;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.mockito.stubbing.Answer1;
29 import org.mockito.stubbing.Answer2;
30 import org.mockito.stubbing.Answer3;
31 import org.mockito.stubbing.Answer4;
32 import org.mockito.stubbing.Answer5;
33 import org.mockito.stubbing.VoidAnswer1;
34 import org.mockito.stubbing.VoidAnswer2;
35 import org.mockito.stubbing.VoidAnswer3;
36 import org.mockito.stubbing.VoidAnswer4;
37 import org.mockito.stubbing.VoidAnswer5;
38 import org.mockitousage.IMethods;
39 
40 @RunWith(MockitoJUnitRunner.class)
41 public class StubbingWithAdditionalAnswersTest {
42 
43     @Mock IMethods iMethods;
44 
45     @Test
can_return_arguments_of_invocation()46     public void can_return_arguments_of_invocation() throws Exception {
47         given(iMethods.objectArgMethod(anyObject())).will(returnsFirstArg());
48         given(iMethods.threeArgumentMethod(eq(0), anyObject(), anyString())).will(returnsSecondArg());
49         given(iMethods.threeArgumentMethod(eq(1), anyObject(), anyString())).will(returnsLastArg());
50 
51         assertThat(iMethods.objectArgMethod("first")).isEqualTo("first");
52         assertThat(iMethods.threeArgumentMethod(0, "second", "whatever")).isEqualTo("second");
53         assertThat(iMethods.threeArgumentMethod(1, "whatever", "last")).isEqualTo("last");
54     }
55 
56     @Test
can_return_expanded_arguments_of_invocation()57     public void can_return_expanded_arguments_of_invocation() throws Exception {
58         given(iMethods.varargsObject(eq(1), anyVararg())).will(returnsArgAt(3));
59 
60         assertThat(iMethods.varargsObject(1, "bob", "alexander", "alice", "carl")).isEqualTo("alice");
61     }
62 
63     @Test
can_return_primitives_or_wrappers()64     public void can_return_primitives_or_wrappers() throws Exception {
65         given(iMethods.toIntPrimitive(anyInt())).will(returnsFirstArg());
66         given(iMethods.toIntWrapper(anyInt())).will(returnsFirstArg());
67 
68         assertThat(iMethods.toIntPrimitive(1)).isEqualTo(1);
69         assertThat(iMethods.toIntWrapper(1)).isEqualTo(1);
70     }
71 
72     @Test
can_return_based_on_strongly_types_one_parameter_function()73     public void can_return_based_on_strongly_types_one_parameter_function() throws Exception {
74         given(iMethods.simpleMethod(anyString()))
75                 .will(answer(new Answer1<String, String>() {
76                     public String answer(String s) {
77                         return s;
78                     }
79                 }));
80 
81         assertThat(iMethods.simpleMethod("string")).isEqualTo("string");
82     }
83 
84     @Test
will_execute_a_void_based_on_strongly_typed_one_parameter_function()85     public void will_execute_a_void_based_on_strongly_typed_one_parameter_function() throws Exception {
86         final IMethods target = mock(IMethods.class);
87 
88         given(iMethods.simpleMethod(anyString()))
89                 .will(answerVoid(new VoidAnswer1<String>() {
90                     public void answer(String s) {
91                         target.simpleMethod(s);
92                     }
93                 }));
94 
95         // invoke on iMethods
96         iMethods.simpleMethod("string");
97 
98         // expect the answer to write correctly to "target"
99         verify(target, times(1)).simpleMethod("string");
100     }
101 
102     @Test
can_return_based_on_strongly_typed_two_parameter_function()103     public void can_return_based_on_strongly_typed_two_parameter_function() throws Exception {
104         given(iMethods.simpleMethod(anyString(), anyInt()))
105             .will(answer(new Answer2<String, String, Integer>() {
106                 public String answer(String s, Integer i) {
107                     return s + "-" + i;
108                 }
109             }));
110 
111         assertThat(iMethods.simpleMethod("string",1)).isEqualTo("string-1");
112     }
113 
114     @Test
will_execute_a_void_based_on_strongly_typed_two_parameter_function()115     public void will_execute_a_void_based_on_strongly_typed_two_parameter_function() throws Exception {
116         final IMethods target = mock(IMethods.class);
117 
118         given(iMethods.simpleMethod(anyString(), anyInt()))
119             .will(answerVoid(new VoidAnswer2<String, Integer>() {
120                 public void answer(String s, Integer i) {
121                     target.simpleMethod(s, i);
122                 }
123             }));
124 
125         // invoke on iMethods
126         iMethods.simpleMethod("string",1);
127 
128         // expect the answer to write correctly to "target"
129         verify(target, times(1)).simpleMethod("string", 1);
130     }
131 
132     @Test
can_return_based_on_strongly_typed_three_parameter_function()133     public void can_return_based_on_strongly_typed_three_parameter_function() throws Exception {
134         final IMethods target = mock(IMethods.class);
135         given(iMethods.threeArgumentMethodWithStrings(anyInt(), anyString(), anyString()))
136                 .will(answer(new Answer3<String, Integer, String, String>() {
137                     public String answer(Integer i, String s1, String s2) {
138                         target.threeArgumentMethodWithStrings(i, s1, s2);
139                         return "answered";
140                     }
141                 }));
142 
143         assertThat(iMethods.threeArgumentMethodWithStrings(1, "string1", "string2")).isEqualTo("answered");
144         verify(target, times(1)).threeArgumentMethodWithStrings(1, "string1", "string2");
145     }
146 
147     @Test
will_execute_a_void_based_on_strongly_typed_three_parameter_function()148     public void will_execute_a_void_based_on_strongly_typed_three_parameter_function() throws Exception {
149         final IMethods target = mock(IMethods.class);
150 
151         given(iMethods.threeArgumentMethodWithStrings(anyInt(), anyString(), anyString()))
152                 .will(answerVoid(new VoidAnswer3<Integer, String, String>() {
153                     public void answer(Integer i, String s1, String s2) {
154                         target.threeArgumentMethodWithStrings(i, s1, s2);
155                     }
156                 }));
157 
158         // invoke on iMethods
159         iMethods.threeArgumentMethodWithStrings(1, "string1", "string2");
160 
161         // expect the answer to write correctly to "target"
162         verify(target, times(1)).threeArgumentMethodWithStrings(1, "string1", "string2");
163     }
164 
165     @Test
can_return_based_on_strongly_typed_four_parameter_function()166         public void can_return_based_on_strongly_typed_four_parameter_function() throws Exception {
167         final IMethods target = mock(IMethods.class);
168         given(iMethods.fourArgumentMethod(anyInt(), anyString(), anyString(), any(boolean[].class)))
169                 .will(answer(new Answer4<String, Integer, String, String, boolean[]>() {
170                     public String answer(Integer i, String s1, String s2, boolean[] a) {
171                         target.fourArgumentMethod(i, s1, s2, a);
172                         return "answered";
173                     }
174                 }));
175 
176         boolean[] booleanArray = { true, false };
177         assertThat(iMethods.fourArgumentMethod(1, "string1", "string2", booleanArray)).isEqualTo("answered");
178         verify(target, times(1)).fourArgumentMethod(1, "string1", "string2", booleanArray);
179     }
180 
181     @Test
will_execute_a_void_based_on_strongly_typed_four_parameter_function()182     public void will_execute_a_void_based_on_strongly_typed_four_parameter_function() throws Exception {
183         final IMethods target = mock(IMethods.class);
184 
185         given(iMethods.fourArgumentMethod(anyInt(), anyString(), anyString(), any(boolean[].class)))
186                 .will(answerVoid(new VoidAnswer4<Integer, String, String, boolean[]>() {
187                     public void answer(Integer i, String s1, String s2, boolean[] a) {
188                         target.fourArgumentMethod(i, s1, s2, a);
189                     }
190                 }));
191 
192         // invoke on iMethods
193         boolean[] booleanArray = { true, false };
194         iMethods.fourArgumentMethod(1, "string1", "string2", booleanArray);
195 
196         // expect the answer to write correctly to "target"
197         verify(target, times(1)).fourArgumentMethod(1, "string1", "string2", booleanArray);
198     }
199 
200     @Test
can_return_based_on_strongly_typed_five_parameter_function()201     public void can_return_based_on_strongly_typed_five_parameter_function() throws Exception {
202         final IMethods target = mock(IMethods.class);
203         given(iMethods.simpleMethod(anyString(), anyInt(), anyInt(), anyInt(), anyInt()))
204                 .will(answer(new Answer5<String, String, Integer, Integer, Integer, Integer>() {
205                     public String answer(String s1, Integer i1, Integer i2, Integer i3, Integer i4) {
206                         target.simpleMethod(s1, i1, i2, i3, i4);
207                         return "answered";
208                     }
209                 }));
210 
211         assertThat(iMethods.simpleMethod("hello", 1, 2, 3, 4)).isEqualTo("answered");
212         verify(target, times(1)).simpleMethod("hello", 1, 2, 3, 4);
213     }
214 
215     @Test
will_execute_a_void_based_on_strongly_typed_five_parameter_function()216     public void will_execute_a_void_based_on_strongly_typed_five_parameter_function() throws Exception {
217         final IMethods target = mock(IMethods.class);
218 
219         given(iMethods.simpleMethod(anyString(), anyInt(), anyInt(), anyInt(), anyInt()))
220                 .will(answerVoid(new VoidAnswer5<String, Integer, Integer, Integer, Integer>() {
221                     public void  answer(String s1, Integer i1, Integer i2, Integer i3, Integer i4) {
222                         target.simpleMethod(s1, i1, i2, i3, i4);
223                     }
224                 }));
225 
226         // invoke on iMethods
227         iMethods.simpleMethod("hello", 1, 2, 3, 4);
228 
229         // expect the answer to write correctly to "target"
230         verify(target, times(1)).simpleMethod("hello", 1, 2, 3, 4);
231     }
232 
233 }
234