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