1 package org.mockitousage.matchers; 2 3 import static org.junit.Assert.fail; 4 import static org.mockito.ArgumentMatchers.any; 5 import static org.mockito.ArgumentMatchers.eq; 6 import static org.mockito.ArgumentMatchers.isNotNull; 7 import static org.mockito.ArgumentMatchers.isNull; 8 import static org.mockito.Mockito.verify; 9 import static org.mockito.Mockito.when; 10 11 import org.assertj.core.api.AbstractListAssert; 12 import org.assertj.core.api.Assertions; 13 import org.assertj.core.api.Condition; 14 import org.junit.Ignore; 15 import org.junit.Rule; 16 import org.junit.Test; 17 import org.junit.rules.ExpectedException; 18 import org.mockito.ArgumentCaptor; 19 import org.mockito.Captor; 20 import org.mockito.Mock; 21 import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent; 22 import org.mockito.junit.MockitoJUnit; 23 import org.mockito.junit.MockitoRule; 24 import org.mockitousage.IMethods; 25 26 public class VarargsTest { 27 28 @Rule 29 public MockitoRule mockitoRule = MockitoJUnit.rule(); 30 @Rule 31 public ExpectedException exception = ExpectedException.none(); 32 @Captor 33 private ArgumentCaptor<String> captor; 34 @Mock 35 private IMethods mock; 36 37 private static final Condition<Object> NULL = new Condition<Object>() { 38 39 @Override 40 public boolean matches(Object value) { 41 return value == null; 42 } 43 }; 44 45 @Test 46 public void shouldMatchVarArgs_noArgs() { 47 mock.varargs(); 48 49 verify(mock).varargs(); 50 } 51 52 @Test 53 @Ignore("This test must succeed but is fails currently, see github issue #616") 54 public void shouldMatchEmptyVarArgs_noArgsIsNotNull() { 55 mock.varargs(); 56 57 verify(mock).varargs(isNotNull()); 58 } 59 60 @Test 61 @Ignore("This test must succeed but is fails currently, see github issue #616") 62 public void shouldMatchEmptyVarArgs_noArgsIsNull() { 63 mock.varargs(); 64 65 verify(mock).varargs(isNull()); 66 } 67 68 @Test 69 @Ignore("This test must succeed but is fails currently, see github issue #616") 70 public void shouldMatchEmptyVarArgs_noArgsIsNotNullArray() { 71 mock.varargs(); 72 73 verify(mock).varargs((String[]) isNotNull()); 74 } 75 76 @Test 77 public void shouldMatchVarArgs_oneNullArg_eqNull() { 78 Object arg = null; 79 mock.varargs(arg); 80 81 verify(mock).varargs(eq(null)); 82 } 83 84 @Test 85 public void shouldMatchVarArgs_oneNullArg_isNull() { 86 Object arg = null; 87 mock.varargs(arg); 88 89 verify(mock).varargs(isNull()); 90 } 91 92 @Test 93 public void shouldMatchVarArgs_nullArrayArg() { 94 Object[] argArray = null; 95 mock.varargs(argArray); 96 97 verify(mock).varargs(isNull()); 98 } 99 100 @Test 101 public void shouldnotMatchVarArgs_twoArgsOneMatcher() { 102 mock.varargs("1", "1"); 103 104 exception.expectMessage("Argument(s) are different"); 105 106 verify(mock).varargs(eq("1")); 107 } 108 109 @Test 110 public void shouldMatchVarArgs_emptyVarArgsOneAnyMatcher() { 111 mock.varargs(); 112 113 verify(mock).varargs((String[])any()); // any() -> VarargMatcher 114 } 115 116 @Test 117 public void shouldMatchVarArgs_oneArgsOneAnyMatcher() { 118 mock.varargs(1); 119 120 verify(mock).varargs(any()); // any() -> VarargMatcher 121 } 122 123 @Test 124 public void shouldMatchVarArgs_twoArgsOneAnyMatcher() { 125 mock.varargs(1, 2); 126 127 verify(mock).varargs(any()); // any() -> VarargMatcher 128 } 129 130 @Test 131 public void shouldMatchVarArgs_twoArgsTwoAnyMatcher() { 132 mock.varargs(1, 2); 133 134 verify(mock).varargs(any(), any()); // any() -> VarargMatcher 135 } 136 137 @Test 138 public void shouldMatchVarArgs_twoArgsThreeAnyMatcher() { 139 mock.varargs(1, 2); 140 141 exception.expectMessage("Argument(s) are different"); 142 143 verify(mock).varargs(any(), any(), any()); //any() -> VarargMatcher 144 } 145 146 @Test 147 public void shouldMatchVarArgs_oneNullArgument() { 148 mock.varargs("1", null); 149 150 verify(mock).varargs(eq("1"), (String) isNull()); 151 } 152 153 @Test 154 public void shouldMatchVarArgs_onebyte() { 155 mock.varargsbyte((byte) 1); 156 157 verify(mock).varargsbyte(eq((byte) 1)); 158 } 159 160 @Test 161 public void shouldMatchVarArgs_nullByteArray() { 162 mock.varargsbyte(null); 163 164 verify(mock).varargsbyte((byte[]) isNull()); 165 } 166 167 @Test 168 public void shouldMatchVarArgs_emptyByteArray() { 169 mock.varargsbyte(); 170 171 verify(mock).varargsbyte(); 172 } 173 174 @Test 175 @Ignore 176 public void shouldMatchEmptyVarArgs_emptyArrayIsNotNull() { 177 mock.varargsbyte(); 178 179 verify(mock).varargsbyte((byte[]) isNotNull()); 180 } 181 182 @Test 183 public void shouldMatchVarArgs_oneArgIsNotNull() { 184 mock.varargsbyte((byte) 1); 185 186 verify(mock).varargsbyte((byte[]) isNotNull()); 187 } 188 189 @Test 190 public void shouldCaptureVarArgs_noArgs() { 191 mock.varargs(); 192 193 verify(mock).varargs(captor.capture()); 194 195 assertThat(captor).isEmpty(); 196 } 197 198 @Test 199 public void shouldCaptureVarArgs_oneNullArg_eqNull() { 200 String arg = null; 201 mock.varargs(arg); 202 203 verify(mock).varargs(captor.capture()); 204 205 assertThat(captor).areExactly(1, NULL); 206 } 207 208 /** 209 * Relates to Github issue #583 "ArgumentCaptor: NPE when an null array is 210 * passed to a varargs method" 211 */ 212 @Test 213 public void shouldCaptureVarArgs_nullArrayArg() { 214 String[] argArray = null; 215 mock.varargs(argArray); 216 217 verify(mock).varargs(captor.capture()); 218 assertThat(captor).areExactly(1, NULL); 219 } 220 221 @Test 222 public void shouldCaptureVarArgs_twoArgsOneCapture() { 223 mock.varargs("1", "2"); 224 225 verify(mock).varargs(captor.capture()); 226 227 assertThat(captor).contains("1", "2"); 228 } 229 230 @Test 231 public void shouldCaptureVarArgs_twoArgsTwoCaptures() { 232 mock.varargs("1", "2"); 233 234 verify(mock).varargs(captor.capture(), captor.capture()); 235 236 assertThat(captor).contains("1", "2"); 237 } 238 239 @Test 240 public void shouldCaptureVarArgs_oneNullArgument() { 241 mock.varargs("1", null); 242 243 verify(mock).varargs(captor.capture()); 244 245 assertThat(captor).contains("1", (String) null); 246 } 247 248 @Test 249 public void shouldCaptureVarArgs_oneNullArgument2() { 250 mock.varargs("1", null); 251 252 verify(mock).varargs(captor.capture(), captor.capture()); 253 254 assertThat(captor).contains("1", (String) null); 255 } 256 257 @Test 258 public void shouldNotCaptureVarArgs_3args2captures() { 259 mock.varargs("1", "2", "3"); 260 261 exception.expect(ArgumentsAreDifferent.class); 262 263 verify(mock).varargs(captor.capture(), captor.capture()); 264 265 } 266 267 @Test 268 public void shouldCaptureVarArgs_3argsCaptorMatcherMix() { 269 mock.varargs("1", "2", "3"); 270 271 verify(mock).varargs(captor.capture(), eq("2"), captor.capture()); 272 273 assertThat(captor).containsExactly("1", "3"); 274 275 } 276 277 @Test 278 public void shouldNotCaptureVarArgs_3argsCaptorMatcherMix() { 279 mock.varargs("1", "2", "3"); 280 281 try { 282 verify(mock).varargs(captor.capture(), eq("X"), captor.capture()); 283 fail("The verification must fail, cause the second arg was not 'X' as expected!"); 284 } catch (ArgumentsAreDifferent expected) { 285 } 286 287 assertThat(captor).isEmpty(); 288 289 } 290 291 @Test 292 public void shouldNotCaptureVarArgs_1args2captures() { 293 mock.varargs("1"); 294 295 exception.expect(ArgumentsAreDifferent.class); 296 297 verify(mock).varargs(captor.capture(), captor.capture()); 298 299 } 300 301 /** 302 * As of v2.0.0-beta.118 this test fails. Once the github issues: 303 * <ul> 304 * <li>'#584 ArgumentCaptor can't capture varargs-arrays 305 * <li>#565 ArgumentCaptor should be type aware' are fixed this test must 306 * succeed 307 * </ul> 308 * 309 * @throws Exception 310 */ 311 @Test 312 @Ignore("Blocked by github issue: #584 & #565") 313 public void shouldCaptureVarArgsAsArray() { 314 mock.varargs("1", "2"); 315 316 ArgumentCaptor<String[]> varargCaptor = ArgumentCaptor.forClass(String[].class); 317 318 verify(mock).varargs(varargCaptor.capture()); 319 320 assertThat(varargCaptor).containsExactly(new String[] { "1", "2" }); 321 } 322 323 @Test 324 public void shouldNotMatchRegualrAndVaraArgs() { 325 mock.varargsString(1, "a","b"); 326 327 exception.expect(ArgumentsAreDifferent.class); 328 329 verify(mock).varargsString(1); 330 } 331 @Test 332 public void shouldNotMatchVaraArgs() { 333 when(mock.varargsObject(1, "a","b")).thenReturn("OK"); 334 335 Assertions.assertThat(mock.varargsObject(1)).isNull(); 336 } 337 338 private static <T> AbstractListAssert<?, ?, T> assertThat(ArgumentCaptor<T> captor) { 339 return Assertions.assertThat(captor.getAllValues()); 340 } 341 } 342