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