How to use test3 method of org.mockito.Mockito class

Best Mockito code snippet using org.mockito.Mockito.test3

Source:TestLoadBalancingKMSClientProvider.java Github

copy

Full Screen

...120 KeyProvider kp = new LoadBalancingKMSClientProvider(121 new KMSClientProvider[] { p1, p2, p3 }, 0, conf);122 assertEquals("p1", kp.createKey("test1", new Options(conf)).getName());123 assertEquals("p2", kp.createKey("test2", new Options(conf)).getName());124 assertEquals("p3", kp.createKey("test3", new Options(conf)).getName());125 assertEquals("p1", kp.createKey("test4", new Options(conf)).getName());126 }127 @Test128 public void testLoadBalancingWithFailure() throws Exception {129 Configuration conf = new Configuration();130 KMSClientProvider p1 = mock(KMSClientProvider.class);131 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))132 .thenReturn(133 new KMSClientProvider.KMSKeyVersion("p1", "v1", new byte[0]));134 when(p1.getKMSUrl()).thenReturn("p1");135 // This should not be retried136 KMSClientProvider p2 = mock(KMSClientProvider.class);137 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))138 .thenThrow(new NoSuchAlgorithmException("p2"));139 when(p2.getKMSUrl()).thenReturn("p2");140 KMSClientProvider p3 = mock(KMSClientProvider.class);141 when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class)))142 .thenReturn(143 new KMSClientProvider.KMSKeyVersion("p3", "v3", new byte[0]));144 when(p3.getKMSUrl()).thenReturn("p3");145 // This should be retried146 KMSClientProvider p4 = mock(KMSClientProvider.class);147 when(p4.createKey(Mockito.anyString(), Mockito.any(Options.class)))148 .thenThrow(new IOException("p4"));149 when(p4.getKMSUrl()).thenReturn("p4");150 KeyProvider kp = new LoadBalancingKMSClientProvider(151 new KMSClientProvider[] { p1, p2, p3, p4 }, 0, conf);152 assertEquals("p1", kp.createKey("test4", new Options(conf)).getName());153 // Exceptions other than IOExceptions will not be retried154 try {155 kp.createKey("test1", new Options(conf)).getName();156 fail("Should fail since its not an IOException");157 } catch (Exception e) {158 assertTrue(e instanceof NoSuchAlgorithmException);159 }160 assertEquals("p3", kp.createKey("test2", new Options(conf)).getName());161 // IOException will trigger retry in next provider162 assertEquals("p1", kp.createKey("test3", new Options(conf)).getName());163 }164 @Test165 public void testLoadBalancingWithAllBadNodes() throws Exception {166 Configuration conf = new Configuration();167 KMSClientProvider p1 = mock(KMSClientProvider.class);168 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))169 .thenThrow(new IOException("p1"));170 KMSClientProvider p2 = mock(KMSClientProvider.class);171 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))172 .thenThrow(new IOException("p2"));173 KMSClientProvider p3 = mock(KMSClientProvider.class);174 when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class)))175 .thenThrow(new IOException("p3"));176 KMSClientProvider p4 = mock(KMSClientProvider.class);177 when(p4.createKey(Mockito.anyString(), Mockito.any(Options.class)))178 .thenThrow(new IOException("p4"));179 when(p1.getKMSUrl()).thenReturn("p1");180 when(p2.getKMSUrl()).thenReturn("p2");181 when(p3.getKMSUrl()).thenReturn("p3");182 when(p4.getKMSUrl()).thenReturn("p4");183 KeyProvider kp = new LoadBalancingKMSClientProvider(184 new KMSClientProvider[] { p1, p2, p3, p4 }, 0, conf);185 try {186 kp.createKey("test3", new Options(conf)).getName();187 fail("Should fail since all providers threw an IOException");188 } catch (Exception e) {189 assertTrue(e instanceof IOException);190 }191 }192 // copied from HttpExceptionUtils:193 // trick, riding on generics to throw an undeclared exception194 private static void throwEx(Throwable ex) {195 TestLoadBalancingKMSClientProvider.<RuntimeException>throwException(ex);196 }197 @SuppressWarnings("unchecked")198 private static <E extends Throwable> void throwException(Throwable ex)199 throws E {200 throw (E) ex;201 }202 private class MyKMSClientProvider extends KMSClientProvider {203 public MyKMSClientProvider(URI uri, Configuration conf) throws IOException {204 super(uri, conf);205 }206 @Override207 public EncryptedKeyVersion generateEncryptedKey(208 final String encryptionKeyName)209 throws IOException, GeneralSecurityException {210 throwEx(new AuthenticationException("bar"));211 return null;212 }213 @Override214 public KeyVersion decryptEncryptedKey(215 final EncryptedKeyVersion encryptedKeyVersion) throws IOException,216 GeneralSecurityException {217 throwEx(new AuthenticationException("bar"));218 return null;219 }220 @Override221 public KeyVersion createKey(final String name, final Options options)222 throws NoSuchAlgorithmException, IOException {223 throwEx(new AuthenticationException("bar"));224 return null;225 }226 @Override227 public KeyVersion rollNewVersion(final String name)228 throws NoSuchAlgorithmException, IOException {229 throwEx(new AuthenticationException("bar"));230 return null;231 }232 }233 @Test234 public void testClassCastException() throws Exception {235 Configuration conf = new Configuration();236 KMSClientProvider p1 = new MyKMSClientProvider(237 new URI("kms://http@host1:9600/kms/foo"), conf);238 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(239 new KMSClientProvider[] {p1}, 0, conf);240 try {241 kp.generateEncryptedKey("foo");242 } catch (IOException ioe) {243 assertTrue(ioe.getCause().getClass().getName().contains(244 "AuthenticationException"));245 }246 try {247 final KeyProviderCryptoExtension.EncryptedKeyVersion248 encryptedKeyVersion =249 mock(KeyProviderCryptoExtension.EncryptedKeyVersion.class);250 kp.decryptEncryptedKey(encryptedKeyVersion);251 } catch (IOException ioe) {252 assertTrue(ioe.getCause().getClass().getName().contains(253 "AuthenticationException"));254 }255 try {256 final KeyProvider.Options options = KeyProvider.options(conf);257 kp.createKey("foo", options);258 } catch (IOException ioe) {259 assertTrue(ioe.getCause().getClass().getName().contains(260 "AuthenticationException"));261 }262 try {263 kp.rollNewVersion("foo");264 } catch (IOException ioe) {265 assertTrue(ioe.getCause().getClass().getName().contains(266 "AuthenticationException"));267 }268 }269 /**270 * tests {@link LoadBalancingKMSClientProvider#warmUpEncryptedKeys(String...)}271 * error handling in case when all the providers throws {@link IOException}.272 * @throws Exception273 */274 @Test275 public void testWarmUpEncryptedKeysWhenAllProvidersFail() throws Exception {276 Configuration conf = new Configuration();277 KMSClientProvider p1 = mock(KMSClientProvider.class);278 String keyName = "key1";279 Mockito.doThrow(new IOException(new AuthorizationException("p1"))).when(p1)280 .warmUpEncryptedKeys(Mockito.anyString());281 KMSClientProvider p2 = mock(KMSClientProvider.class);282 Mockito.doThrow(new IOException(new AuthorizationException("p2"))).when(p2)283 .warmUpEncryptedKeys(Mockito.anyString());284 when(p1.getKMSUrl()).thenReturn("p1");285 when(p2.getKMSUrl()).thenReturn("p2");286 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(287 new KMSClientProvider[] {p1, p2}, 0, conf);288 try {289 kp.warmUpEncryptedKeys(keyName);290 fail("Should fail since both providers threw IOException");291 } catch (Exception e) {292 assertTrue(e.getCause() instanceof IOException);293 }294 Mockito.verify(p1, Mockito.times(1)).warmUpEncryptedKeys(keyName);295 Mockito.verify(p2, Mockito.times(1)).warmUpEncryptedKeys(keyName);296 }297 /**298 * tests {@link LoadBalancingKMSClientProvider#warmUpEncryptedKeys(String...)}299 * error handling in case atleast one provider succeeds.300 * @throws Exception301 */302 @Test303 public void testWarmUpEncryptedKeysWhenOneProviderSucceeds()304 throws Exception {305 Configuration conf = new Configuration();306 KMSClientProvider p1 = mock(KMSClientProvider.class);307 String keyName = "key1";308 Mockito.doThrow(new IOException(new AuthorizationException("p1"))).when(p1)309 .warmUpEncryptedKeys(Mockito.anyString());310 KMSClientProvider p2 = mock(KMSClientProvider.class);311 Mockito.doNothing().when(p2)312 .warmUpEncryptedKeys(Mockito.anyString());313 when(p1.getKMSUrl()).thenReturn("p1");314 when(p2.getKMSUrl()).thenReturn("p2");315 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(316 new KMSClientProvider[] {p1, p2}, 0, conf);317 try {318 kp.warmUpEncryptedKeys(keyName);319 } catch (Exception e) {320 fail("Should not throw Exception since p2 doesn't throw Exception");321 }322 Mockito.verify(p1, Mockito.times(1)).warmUpEncryptedKeys(keyName);323 Mockito.verify(p2, Mockito.times(1)).warmUpEncryptedKeys(keyName);324 }325 /**326 * Tests whether retryPolicy fails immediately on non-idempotent operations,327 * after trying each provider once,328 * on encountering IOException which is not SocketException.329 * @throws Exception330 */331 @Test332 public void testClientRetriesNonIdempotentOpWithIOExceptionFailsImmediately()333 throws Exception {334 Configuration conf = new Configuration();335 final String keyName = "test";336 // Setting total failover attempts to .337 conf.setInt(338 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 10);339 KMSClientProvider p1 = mock(KMSClientProvider.class);340 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))341 .thenThrow(new IOException("p1"));342 KMSClientProvider p2 = mock(KMSClientProvider.class);343 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))344 .thenThrow(new IOException("p2"));345 KMSClientProvider p3 = mock(KMSClientProvider.class);346 when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class)))347 .thenThrow(new IOException("p3"));348 when(p1.getKMSUrl()).thenReturn("p1");349 when(p2.getKMSUrl()).thenReturn("p2");350 when(p3.getKMSUrl()).thenReturn("p3");351 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(352 new KMSClientProvider[] {p1, p2, p3}, 0, conf);353 try {354 kp.createKey(keyName, new Options(conf));355 fail("Should fail since all providers threw an IOException");356 } catch (Exception e) {357 assertTrue(e instanceof IOException);358 }359 verify(kp.getProviders()[0], Mockito.times(1))360 .createKey(Mockito.eq(keyName), Mockito.any(Options.class));361 verify(kp.getProviders()[1], Mockito.times(1))362 .createKey(Mockito.eq(keyName), Mockito.any(Options.class));363 verify(kp.getProviders()[2], Mockito.times(1))364 .createKey(Mockito.eq(keyName), Mockito.any(Options.class));365 }366 /**367 * Tests whether retryPolicy retries on idempotent operations368 * when encountering IOException.369 * @throws Exception370 */371 @Test372 public void testClientRetriesIdempotentOpWithIOExceptionSucceedsSecondTime()373 throws Exception {374 Configuration conf = new Configuration();375 final String keyName = "test";376 final KeyProvider.KeyVersion keyVersion377 = new KMSClientProvider.KMSKeyVersion(keyName, "v1",378 new byte[0]);379 // Setting total failover attempts to .380 conf.setInt(381 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 10);382 KMSClientProvider p1 = mock(KMSClientProvider.class);383 when(p1.getCurrentKey(Mockito.anyString()))384 .thenThrow(new IOException("p1"))385 .thenReturn(keyVersion);386 KMSClientProvider p2 = mock(KMSClientProvider.class);387 when(p2.getCurrentKey(Mockito.anyString()))388 .thenThrow(new IOException("p2"));389 KMSClientProvider p3 = mock(KMSClientProvider.class);390 when(p3.getCurrentKey(Mockito.anyString()))391 .thenThrow(new IOException("p3"));392 when(p1.getKMSUrl()).thenReturn("p1");393 when(p2.getKMSUrl()).thenReturn("p2");394 when(p3.getKMSUrl()).thenReturn("p3");395 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(396 new KMSClientProvider[] {p1, p2, p3}, 0, conf);397 KeyProvider.KeyVersion result = kp.getCurrentKey(keyName);398 assertEquals(keyVersion, result);399 verify(kp.getProviders()[0], Mockito.times(2))400 .getCurrentKey(Mockito.eq(keyName));401 verify(kp.getProviders()[1], Mockito.times(1))402 .getCurrentKey(Mockito.eq(keyName));403 verify(kp.getProviders()[2], Mockito.times(1))404 .getCurrentKey(Mockito.eq(keyName));405 }406 /**407 * Tests that client doesn't retry once it encounters AccessControlException408 * from first provider.409 * This assumes all the kms servers are configured with identical access to410 * keys.411 * @throws Exception412 */413 @Test414 public void testClientRetriesWithAccessControlException() throws Exception {415 Configuration conf = new Configuration();416 conf.setInt(417 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 3);418 KMSClientProvider p1 = mock(KMSClientProvider.class);419 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))420 .thenThrow(new AccessControlException("p1"));421 KMSClientProvider p2 = mock(KMSClientProvider.class);422 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))423 .thenThrow(new IOException("p2"));424 KMSClientProvider p3 = mock(KMSClientProvider.class);425 when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class)))426 .thenThrow(new IOException("p3"));427 when(p1.getKMSUrl()).thenReturn("p1");428 when(p2.getKMSUrl()).thenReturn("p2");429 when(p3.getKMSUrl()).thenReturn("p3");430 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(431 new KMSClientProvider[] {p1, p2, p3}, 0, conf);432 try {433 kp.createKey("test3", new Options(conf));434 fail("Should fail because provider p1 threw an AccessControlException");435 } catch (Exception e) {436 assertTrue(e instanceof AccessControlException);437 }438 verify(p1, Mockito.times(1)).createKey(Mockito.eq("test3"),439 Mockito.any(Options.class));440 verify(p2, Mockito.never()).createKey(Mockito.eq("test3"),441 Mockito.any(Options.class));442 verify(p3, Mockito.never()).createKey(Mockito.eq("test3"),443 Mockito.any(Options.class));444 }445 /**446 * Tests that client doesn't retry once it encounters RunTimeException447 * from first provider.448 * This assumes all the kms servers are configured with identical access to449 * keys.450 * @throws Exception451 */452 @Test453 public void testClientRetriesWithRuntimeException() throws Exception {454 Configuration conf = new Configuration();455 conf.setInt(456 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 3);457 KMSClientProvider p1 = mock(KMSClientProvider.class);458 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))459 .thenThrow(new RuntimeException("p1"));460 KMSClientProvider p2 = mock(KMSClientProvider.class);461 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))462 .thenThrow(new IOException("p2"));463 when(p1.getKMSUrl()).thenReturn("p1");464 when(p2.getKMSUrl()).thenReturn("p2");465 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(466 new KMSClientProvider[] {p1, p2}, 0, conf);467 try {468 kp.createKey("test3", new Options(conf));469 fail("Should fail since provider p1 threw RuntimeException");470 } catch (Exception e) {471 assertTrue(e instanceof RuntimeException);472 }473 verify(p1, Mockito.times(1)).createKey(Mockito.eq("test3"),474 Mockito.any(Options.class));475 verify(p2, Mockito.never()).createKey(Mockito.eq("test3"),476 Mockito.any(Options.class));477 }478 /**479 * Tests the client retries until it finds a good provider.480 * @throws Exception481 */482 @Test483 public void testClientRetriesWithTimeoutsException() throws Exception {484 Configuration conf = new Configuration();485 conf.setInt(486 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 4);487 KMSClientProvider p1 = mock(KMSClientProvider.class);488 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))489 .thenThrow(new ConnectTimeoutException("p1"));490 KMSClientProvider p2 = mock(KMSClientProvider.class);491 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))492 .thenThrow(new UnknownHostException("p2"));493 KMSClientProvider p3 = mock(KMSClientProvider.class);494 when(p3.createKey(Mockito.anyString(), Mockito.any(Options.class)))495 .thenThrow(new NoRouteToHostException("p3"));496 KMSClientProvider p4 = mock(KMSClientProvider.class);497 when(p4.createKey(Mockito.anyString(), Mockito.any(Options.class)))498 .thenReturn(499 new KMSClientProvider.KMSKeyVersion("test3", "v1", new byte[0]));500 when(p1.getKMSUrl()).thenReturn("p1");501 when(p2.getKMSUrl()).thenReturn("p2");502 when(p3.getKMSUrl()).thenReturn("p3");503 when(p4.getKMSUrl()).thenReturn("p4");504 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(505 new KMSClientProvider[] {p1, p2, p3, p4}, 0, conf);506 try {507 kp.createKey("test3", new Options(conf));508 } catch (Exception e) {509 fail("Provider p4 should have answered the request.");510 }511 verify(p1, Mockito.times(1)).createKey(Mockito.eq("test3"),512 Mockito.any(Options.class));513 verify(p2, Mockito.times(1)).createKey(Mockito.eq("test3"),514 Mockito.any(Options.class));515 verify(p3, Mockito.times(1)).createKey(Mockito.eq("test3"),516 Mockito.any(Options.class));517 verify(p4, Mockito.times(1)).createKey(Mockito.eq("test3"),518 Mockito.any(Options.class));519 }520 /**521 * Tests the operation succeeds second time after ConnectTimeoutException.522 * @throws Exception523 */524 @Test525 public void testClientRetriesSucceedsSecondTime() throws Exception {526 Configuration conf = new Configuration();527 conf.setInt(528 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 3);529 KMSClientProvider p1 = mock(KMSClientProvider.class);530 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))531 .thenThrow(new ConnectTimeoutException("p1"))532 .thenReturn(new KMSClientProvider.KMSKeyVersion("test3", "v1",533 new byte[0]));534 KMSClientProvider p2 = mock(KMSClientProvider.class);535 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))536 .thenThrow(new ConnectTimeoutException("p2"));537 when(p1.getKMSUrl()).thenReturn("p1");538 when(p2.getKMSUrl()).thenReturn("p2");539 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(540 new KMSClientProvider[] {p1, p2}, 0, conf);541 try {542 kp.createKey("test3", new Options(conf));543 } catch (Exception e) {544 fail("Provider p1 should have answered the request second time.");545 }546 verify(p1, Mockito.times(2)).createKey(Mockito.eq("test3"),547 Mockito.any(Options.class));548 verify(p2, Mockito.times(1)).createKey(Mockito.eq("test3"),549 Mockito.any(Options.class));550 }551 /**552 * Tests whether retryPolicy retries specified number of times.553 * @throws Exception554 */555 @Test556 public void testClientRetriesSpecifiedNumberOfTimes() throws Exception {557 Configuration conf = new Configuration();558 conf.setInt(559 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 10);560 KMSClientProvider p1 = mock(KMSClientProvider.class);561 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))562 .thenThrow(new ConnectTimeoutException("p1"));563 KMSClientProvider p2 = mock(KMSClientProvider.class);564 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))565 .thenThrow(new ConnectTimeoutException("p2"));566 when(p1.getKMSUrl()).thenReturn("p1");567 when(p2.getKMSUrl()).thenReturn("p2");568 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(569 new KMSClientProvider[] {p1, p2}, 0, conf);570 try {571 kp.createKey("test3", new Options(conf));572 fail("Should fail");573 } catch (Exception e) {574 assert (e instanceof ConnectTimeoutException);575 }576 verify(p1, Mockito.times(6)).createKey(Mockito.eq("test3"),577 Mockito.any(Options.class));578 verify(p2, Mockito.times(5)).createKey(Mockito.eq("test3"),579 Mockito.any(Options.class));580 }581 /**582 * Tests whether retryPolicy retries number of times equals to number of583 * providers if conf kms.client.failover.max.attempts is not set.584 * @throws Exception585 */586 @Test587 public void testClientRetriesIfMaxAttemptsNotSet() throws Exception {588 Configuration conf = new Configuration();589 KMSClientProvider p1 = mock(KMSClientProvider.class);590 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))591 .thenThrow(new ConnectTimeoutException("p1"));592 KMSClientProvider p2 = mock(KMSClientProvider.class);593 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))594 .thenThrow(new ConnectTimeoutException("p2"));595 when(p1.getKMSUrl()).thenReturn("p1");596 when(p2.getKMSUrl()).thenReturn("p2");597 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(598 new KMSClientProvider[] {p1, p2}, 0, conf);599 try {600 kp.createKey("test3", new Options(conf));601 fail("Should fail");602 } catch (Exception e) {603 assert (e instanceof ConnectTimeoutException);604 }605 verify(p1, Mockito.times(2)).createKey(Mockito.eq("test3"),606 Mockito.any(Options.class));607 verify(p2, Mockito.times(1)).createKey(Mockito.eq("test3"),608 Mockito.any(Options.class));609 }610 /**611 * Tests that client reties each provider once, when it encounters612 * AuthenticationException wrapped in an IOException from first provider.613 * @throws Exception614 */615 @Test616 public void testClientRetriesWithAuthenticationExceptionWrappedinIOException()617 throws Exception {618 Configuration conf = new Configuration();619 conf.setInt(620 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 3);621 KMSClientProvider p1 = mock(KMSClientProvider.class);622 when(p1.createKey(Mockito.anyString(), Mockito.any(Options.class)))623 .thenThrow(new IOException(new AuthenticationException("p1")));624 KMSClientProvider p2 = mock(KMSClientProvider.class);625 when(p2.createKey(Mockito.anyString(), Mockito.any(Options.class)))626 .thenThrow(new IOException(new AuthenticationException("p1")));627 when(p1.getKMSUrl()).thenReturn("p1");628 when(p2.getKMSUrl()).thenReturn("p2");629 LoadBalancingKMSClientProvider kp = new LoadBalancingKMSClientProvider(630 new KMSClientProvider[] {p1, p2}, 0, conf);631 try {632 kp.createKey("test3", new Options(conf));633 fail("Should fail since provider p1 threw AuthenticationException");634 } catch (Exception e) {635 assertTrue(e.getCause() instanceof AuthenticationException);636 }637 verify(p1, Mockito.times(1)).createKey(Mockito.eq("test3"),638 Mockito.any(Options.class));639 verify(p2, Mockito.times(1)).createKey(Mockito.eq("test3"),640 Mockito.any(Options.class));641 }642 /**643 * Tests the operation succeeds second time after SSLHandshakeException.644 * @throws Exception645 */646 @Test647 public void testClientRetriesWithSSLHandshakeExceptionSucceedsSecondTime()648 throws Exception {649 Configuration conf = new Configuration();650 conf.setInt(651 CommonConfigurationKeysPublic.KMS_CLIENT_FAILOVER_MAX_RETRIES_KEY, 3);652 final String keyName = "test";653 KMSClientProvider p1 = mock(KMSClientProvider.class);...

Full Screen

Full Screen

Source:PowerMockVerifyTest.java Github

copy

Full Screen

...35 }36 @Test37 public void testVerify() throws Exception {38 //先执行逻辑, Powermock会收集执行时的数据,比如函数被调用多次,函数执行时间等信息,39 HelloUtils2.test3();40 HelloUtils2.test3();41 HelloUtils2.test3();42 //然后再对Powermock收集到的数据进行校验 , verifyStatic函数的参数是一个校验模型43 // times(3)表示执行了3次, 但是此时还不知道对哪个函数的执行次数校验3次44 // 必须在后面调用 要校验的 函数, 执行后, Powermock就知道要校验谁了,45 //Powermock此时会执行真正的校验逻辑, 看test3函数是否真的执行了3次46 PowerMockito.verifyStatic(times(3));47 HelloUtils2.test3();48 }49 @Test50 public void testVerifyFailed() throws Exception {51 //先执行逻辑, Powermock会收集执行时的数据,比如函数被调用多次,函数执行时间等信息,52 HelloUtils2.test3();53 HelloUtils2.test3();54// HelloUtils2.test3();55 //然后再对Powermock收集到的数据进行校验 , verifyStatic函数的参数是一个校验模型56 // times(3)表示执行了3次, 但是此时还不知道对哪个函数的执行次数校验3次57 // 必须在后面调用 要校验的 函数, 执行后, Powermock就知道要校验谁了,58 //Powermock此时会执行真正的校验逻辑, 看test3函数是否真的执行了3次59 PowerMockito.verifyStatic(times(3));60 HelloUtils2.test3();61 }62 @Test63 public void testVerifyStringParam() throws Exception {64 HelloUtils2.testStringParam("test");65 HelloUtils2.testStringParam("test");66 PowerMockito.verifyStatic(times(2));67 HelloUtils2.testStringParam("test");68 }69 @Test70 public void testVerifyStringParamFailed() throws Exception {71 HelloUtils2.testStringParam("test1");72 HelloUtils2.testStringParam("test");73 PowerMockito.verifyStatic(times(2));74 HelloUtils2.testStringParam("test");...

Full Screen

Full Screen

Source:StaffUnitServiceTest.java Github

copy

Full Screen

...30 test1.setName("Директор");31 StaffUnit test2 = new StaffUnit();32 test2.setId(2L);33 test2.setName("Менеджер");34 StaffUnit test3 = new StaffUnit();35 test3.setId(3L);36 test3.setName("Бухгалтер");37 List<StaffUnit> names = new ArrayList<>();38 names.add(test1);39 names.add(test2);40 names.add(test3);41 //create new42 staffUnitNew = new StaffUnit();43 staffUnitNew.setName("Директор");44 //update45 staffUnitTest = new StaffUnit();46 staffUnitTest.setId(1L);47 staffUnitTest.setName("Директор NEW");48 staffUnitTest.setRoles(null);49 Mockito.doReturn(names).when(staffUnitRepository).findAll();50 Mockito.doReturn(Optional.of(test1)).when(staffUnitRepository).findOneByName(STAFF_UNIT_NAME);51 Mockito.doNothing().when(staffUnitRepository).deleteById(test3.getId());52 Mockito.doAnswer(invocation -> {53 names.remove((int) (test3.getId() - 1));54 return null;55 }).when(staffUnitRepository).deleteById(test3.getId());56 Mockito.doReturn(test1).when(staffUnitRepository).save(staffUnitNew);57 Mockito.doReturn(staffUnitTest).when(staffUnitRepository).save(staffUnitTest);58 Mockito.doReturn(Optional.of(staffUnitTest)).when(staffUnitRepository).findOneByName(STAFF_UNIT_NAME + " NEW");59 }60 @Test61 void getNamesTest() {62 List<String> names = staffUnitService.getNames();63 Assertions.assertNotNull(names);64 Assertions.assertEquals(3, names.size());65 Assertions.assertEquals("Директор", names.get(0));66 Assertions.assertEquals("Менеджер", names.get(1));67 Assertions.assertEquals("Бухгалтер", names.get(2));68 }69 @Test...

Full Screen

Full Screen

Source:FileDeletionQueueTest.java Github

copy

Full Screen

...49 public void testMultipleDeletion() {50 FileDeletionQueue queue = new FileDeletionQueue(mWrappedDeleter);51 queue.delete("test1");52 queue.delete("test2");53 queue.delete("test3");54 mWrappedDeleter.waitFor(3);55 verify(mDeleter, times(1)).onResult("test1");56 verify(mDeleter, times(1)).onResult("test2");57 verify(mDeleter, times(1)).onResult("test3");58 }59 @Test60 public void testMultipleDeletionsAPI() {61 FileDeletionQueue queue = new FileDeletionQueue(mWrappedDeleter);62 queue.delete(CollectionUtil.newArrayList("test1", "test2", "test3"));63 mWrappedDeleter.waitFor(3);64 verify(mDeleter, times(1)).onResult("test1");65 verify(mDeleter, times(1)).onResult("test2");66 verify(mDeleter, times(1)).onResult("test3");67 }68 @Test69 public void testOneDeletionHappensAtATime() {70 FileDeletionQueue queue = new FileDeletionQueue(mWrappedDeleter);71 queue.delete(CollectionUtil.newArrayList("test1", "test2", "test3"));72 mWrappedDeleter.waitFor(1);73 verify(mDeleter, times(1)).onResult("test1");74 mWrappedDeleter.waitFor(1);75 verify(mDeleter, times(1)).onResult("test2");76 mWrappedDeleter.waitFor(1);77 verify(mDeleter, times(1)).onResult("test3");78 }79 private static class CallbackWrapper implements Callback<String> {80 private final Callback<String> mWrappedCallback;81 private final Semaphore mDeletedSemaphore = new Semaphore(0);82 public CallbackWrapper(Callback<String> wrappedCallback) {83 mWrappedCallback = wrappedCallback;84 }85 public void waitFor(int calls) {86 long time = System.currentTimeMillis();87 while (!mDeletedSemaphore.tryAcquire(calls)) {88 if (time - System.currentTimeMillis() > 3000) Assert.fail();89 ShadowLooper.runUiThreadTasksIncludingDelayedTasks();90 Robolectric.flushBackgroundThreadScheduler();91 }...

Full Screen

Full Screen

Source:AdminConverterTest.java Github

copy

Full Screen

...49 void shouldMapAllAdminsToPageOfResponses() {50 Page<AdminUser> payments = new PageImpl<>(List.of(51 new AdminUser("test1@test.pl", "test1", "test1"),52 new AdminUser("test2@test.pl", "test2", "test2"),53 new AdminUser("test3@test.pl", "test3", "test3")54 ));55 Page<AdminResponse> result = adminConverter.convertPageToDto(payments);56 assertThat(result).hasSize(3);57 assertThat(result).extracting(AdminResponse::getFullName)58 .containsExactlyInAnyOrder("test1", "test2", "test3");59 }60 @Test61 void shouldCovertAllAdminsToListDto() {62 List<AdminUser> payments = List.of(63 new AdminUser("test1@test.pl", "test1", "test1"),64 new AdminUser("test2@test.pl", "test2", "test2"),65 new AdminUser("test3@test.pl", "test3", "test3")66 );67 List<AdminResponse> result = adminConverter.convertAllToDto(payments);68 assertThat(result).hasSize(3);69 assertThat(result).extracting(AdminResponse::getFullName)70 .containsExactlyInAnyOrder("test1", "test2", "test3");71 }72}...

Full Screen

Full Screen

Source:TestStPuNVArgsVarArg.java Github

copy

Full Screen

...2324 // 可以分别设置参数数量在不同情况下的处理25 @Test26 public void test1() {27 Mockito.when(TestStaticPublicNonVoid3.test3(Mockito.anyString())).thenReturn(TestConstants28 .FLAG1);2930 Mockito.when(TestStaticPublicNonVoid3.test3(Mockito.anyString(), Mockito.anyString())).thenReturn(TestConstants31 .FLAG2);3233 Mockito.when(TestStaticPublicNonVoid3.test3(Mockito.anyString(), Mockito.anyString(), Mockito.anyString()))34 .thenReturn(TestConstants.FLAG3);3536 String str = TestStaticPublicNonVoid3.test3("");37 assertEquals(TestConstants.FLAG1, str);3839 str = TestStaticPublicNonVoid3.test3("", "");40 assertEquals(TestConstants.FLAG2, str);4142 str = TestStaticPublicNonVoid3.test3("", "", "");43 assertEquals(TestConstants.FLAG3, str);44 }4546 // Mockito.any()方法能够匹配任意数量的任意参数47 @Test48 public void test2() {49 Mockito.when(TestStaticPublicNonVoid3.test3(Mockito.any(), Mockito.any())).thenReturn(TestConstants50 .MOCKED);5152 String str = TestStaticPublicNonVoid3.test3("");53 assertEquals(TestConstants.MOCKED, str);5455 str = TestStaticPublicNonVoid3.test3("", "");56 assertEquals(TestConstants.MOCKED, str);5758 str = TestStaticPublicNonVoid3.test3("", "", "");59 assertEquals(TestConstants.MOCKED, str);60 }6162 @Test63 public void test3() {64 Mockito.when(TestStaticPublicNonVoid3.test3(Mockito.anyString())).thenReturn(TestConstants65 .MOCKED);6667 String str = TestStaticPublicNonVoid3.test3("");68 assertEquals(TestConstants.MOCKED, str);6970 str = TestStaticPublicNonVoid3.test3("", "");71 assertNull(str);72 }7374 @Test75 public void test4() {76 Mockito.when(TestStaticPublicNonVoid3.test3(Mockito.any(String.class))).thenReturn(TestConstants77 .MOCKED);7879 String str = TestStaticPublicNonVoid3.test3("");80 assertEquals(TestConstants.MOCKED, str);8182 str = TestStaticPublicNonVoid3.test3("", "");83 assertNull(str);84 }85} ...

Full Screen

Full Screen

Source:TestSpEffectiveStThrRe1.java Github

copy

Full Screen

...4243 PowerMockito.replace(PowerMockito.method(TestServiceA1Impl.class, TestServiceA1Impl.NAME_TEST3))44 .with((proxy, method, args) -> TestConstants.FLAG2);4546 String str = testServiceA1.test3("");4748 // PowerMockito.replace在PowerMockito.stub.toThrow之后,生效49 assertEquals(TestConstants.FLAG2, str);50 }51} ...

Full Screen

Full Screen

Source:TestSpEffectiveStubThrRe1.java Github

copy

Full Screen

...27 PowerMockito.stub(PowerMockito.method(TestServiceA1Impl.class, TestServiceA1Impl.NAME_TEST3)).toThrow(new28 FileNotFoundException(TestConstants.FLAG2));2930 // PowerMockito.stub.toReturn生效31 String str = testServiceA1.test3("");32 assertEquals(TestConstants.FLAG2, str);33 }34} ...

Full Screen

Full Screen

test3

Using AI Code Generation

copy

Full Screen

1package org.mockito;2public class Mockito {3 public static void test3() {4 System.out.println("test3");5 }6}7package org.mockito;8public class Mockito {9 public static void test4() {10 System.out.println("test4");11 }12}13package org.mockito;14public class Mockito {15 public static void test5() {16 System.out.println("test5");17 }18}19package org.mockito;20public class Mockito {21 public static void test6() {22 System.out.println("test6");23 }24}25package org.mockito;26public class Mockito {27 public static void test7() {28 System.out.println("test7");29 }30}31package org.mockito;32public class Mockito {33 public static void test8() {34 System.out.println("test8");35 }36}37package org.mockito;38public class Mockito {39 public static void test9() {40 System.out.println("test9");41 }42}43package org.mockito;44public class Mockito {45 public static void test10() {46 System.out.println("test10");47 }48}49package org.mockito;50public class Mockito {51 public static void test11() {52 System.out.println("test11");53 }54}55package org.mockito;56public class Mockito {57 public static void test12() {58 System.out.println("test12");59 }60}61package org.mockito;62public class Mockito {63 public static void test13() {64 System.out.println("test13");65 }66}

Full Screen

Full Screen

test3

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2public class test3 {3 public static void main(String[] args) {4 test3 t = new test3();5 t.test3();6 }7 public void test3() {8 System.out.println(Mockito.mockingDetails(this).isMock());9 }10}11import org.mockito.Mockito;12public class test3 {13 public static void main(String[] args) {14 test3 t = Mockito.mock(test3.class);15 t.test3();16 }17 public void test3() {18 System.out.println(Mockito.mockingDetails(this).isMock());19 }20}21Recommended Posts: Mockito - verify() method22Mockito - verifyZeroInteractions() method23Mockito - verifyNoMoreInteractions() method24Mockito - verifyNoInteractions() method25Mockito - verifyNoMoreInteractions() method26Mockito - verifyZeroInteractions() method27Mockito - verify() method28Mockito - when() method29Mockito - doReturn() method30Mockito - doThrow() method31Mockito - doAnswer() method32Mockito - doNothing() method33Mockito - doCallRealMethod() method34Mockito - doNothing() method35Mockito - doAnswer() method36Mockito - doThrow() method37Mockito - doReturn() method38Mockito - when() method39Mockito - verify() method40Mockito - verifyNoInteractions() method41Mockito - verifyNoMoreInteractions() method42Mockito - verifyZeroInteractions() method43Mockito - verify() method44Mockito - verifyZeroInteractions() method45Mockito - verifyNoMoreInteractions() method46Mockito - verifyNoInteractions() method47Mockito - verify() method48Mockito - when() method49Mockito - doReturn() method50Mockito - doThrow() method51Mockito - doAnswer() method52Mockito - doNothing() method53Mockito - doCallRealMethod() method

Full Screen

Full Screen

test3

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito.*;2import org.mockito.Mockito;3public class test3 {4 public static void main(String[] args) {5 List<String> mockList = Mockito.mock(ArrayList.class);6 when(mockList.get(0)).thenReturn("Mockito");7 when(mockList.get(1)).thenReturn("is a mocking framework");8 System.out.println(mockList.get(0));9 System.out.println(mockList.get(1));10 }11}12Example 2: Mockito doReturn() method13import org.mockito.Mockito.*;14public class doReturn {15 public static void main(String[] args) {16 List<String> mockList = Mockito.mock(ArrayList.class);17 doReturn("Mockito").when(mockList).get(0);18 doReturn("is a mocking framework").when(mockList).get(1);19 System.out.println(mockList.get(0));20 System.out.println(mockList.get(1));21 }22}23Example 3: Mockito doThrow() method24import org.mockito.Mockito.*;25public class doThrow {26 public static void main(String[] args) {27 List<String> mockList = Mockito.mock(ArrayList.class);

Full Screen

Full Screen

test3

Using AI Code Generation

copy

Full Screen

1import static org.mockito.Mockito.test3;2import org.mockito.Mockito;3public class Test3 {4 public static void main(String[] args) {5 Mockito mockito = new Mockito();6 mockito.test3();7 }8}9org.mockito.Mockito.test3() Method10public static boolean test3(Object mock, String methodName)11import static org.mockito.Mockito.test3;12import org.mockito.Mockito;13public class Test3 {14 public static void main(String[] args) {15 Mockito mockito = new Mockito();16 mockito.test3();17 }18}19org.mockito.Mockito.test4() Method20public static boolean test4(Object mock, String methodName, Range range)21import static org.mockito.Mockito.test4;22import org.mockito.Mockito;23public class Test4 {24 public static void main(String[] args) {25 Mockito mockito = new Mockito();26 mockito.test4();27 }28}29org.mockito.Mockito.times() Method

Full Screen

Full Screen

test3

Using AI Code Generation

copy

Full Screen

1import static org.mockito.Mockito.*;2import org.mockito.Mockito.*;3import org.mockito.Mockito.*;4public class 1 {5 public static void main(String[] args) {6 1 mock = mock(1.class);7 when(mock.test3()).thenReturn("Hello World");8 System.out.println(mock.test3());9 }10}11Method Description when() It is used to stub the method call with the given value. verify() It is used to verify the method call. doThrow() It is used to stub the method call to throw an exception. doNothing() It is used to stub the method call to do nothing. doCallRealMethod() It is used to stub the method call to call the real method. doAnswer() It is used to stub the method call to call the real method. doReturn() It is used to stub the method call with the given value. doNothing() It is used to stub the method call to do nothing. doThrow() It is used to stub the method call to throw an exception. InOrder It is used to verify the order of the method calls. reset() It is used to reset the mock object. spy() It is used to spy on the real object. verifyNoMoreInteractions() It is used to verify that no more interactions occurred on the mock object. verifyZeroInteractions() It is used to verify that no interactions occurred on the mock object. verifyNoInteractions() It is used to verify that no interactions occurred on the mock object. InOrder It is used to verify the order of the method calls. reset() It is used to reset the mock object. spy() It is used to spy on the real object. verifyNoMoreInteractions() It is used to verify that no more interactions occurred on the mock object. verifyZeroInteractions() It is used to verify that no interactions occurred on the mock

Full Screen

Full Screen

test3

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2public class test3 {3 public static void main(String[] args) {4 System.out.println("test3 method");5 System.out.println(Mockito.mockingDetails("test").isMock());6 }7}8import org.mockito.Mockito;9public class test4 {10 public static void main(String[] args) {11 System.out.println("test4 method");12 System.out.println(Mockito.mockingDetails("test").getMockCreationSettings().getTypeToMock());13 }14}15import org.mockito.Mockito;16public class test5 {17 public static void main(String[] args) {18 System.out.println("test5 method");19 System.out.println(Mockito.mockingDetails("test").getMockCreationSettings().getExtraInterfaces());20 }21}22import org.mockito.Mockito;23public class test6 {24 public static void main(String[] args) {25 System.out.println("test6 method");26 System.out.println(Mockito.mockingDetails("test").getMockCreationSettings().getDefaultAnswer());27 }28}29import org.mockito.Mockito;30public class test7 {31 public static void main(String[] args) {32 System.out.println("test7 method");33 System.out.println(Mockito.mockingDetails("test").getMockCreationSettings().getMockName());34 }35}36import org.mockito.Mockito;37public static class test8 {38 public static void main(String[] args) {39 System.out.println("test8 method");40 System.out.println(Mockito.mockingDetails("test").getMockCreationSettings().getSerializationBehavior());41 }42}

Full Screen

Full Screen

test3

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2class Test {3 public static void main(String[] args) {4 Mockito.test3();5 }6}7Recommended Posts: Java | Mockito test3() method8Java | Mockito test4() method9Java | Mockito test5() method10Java | Mockito test6() method11Java | Mockito test7() method12Java | Mockito test8() method13Java | Mockito test9() method14Java | Mockito test10() method15Java | Mockito test11() method16Java | Mockito test12() method17Java | Mockito test13() method18Java | Mockito test14() method19Java | Mockito test15() method20Java | Mockito test16() method21Java | Mockito test17() method22Java | Mockito test18() method23Java | Mockito test19() method24Java | Mockito test20() method25Java | Mockito test21() method26Java | Mockito test22() method27Java | Mockito test23() method28Java | Mockito test24() method29Java | Mockito test25() method30Java | Mockito test26() method31Java | Mockito test27() method32Java | Mockito test28() method33Java | Mockito test29() method34Java | Mockito test30() method35Java | Mockito test31() method36Java | Mockito test32() method37Java | Mockito test33() method38Java | Mockito test34() method39Java | Mockito test35() method40Java | Mockito test36() method41Java | Mockito test37() method42Java | Mockito test38() method43Java | Mockito test39() method44Java | Mockito test40() method45Java | Mockito test41() method46Java | Mockito test42() method47Java | Mockito test43() method48Java | Mockito test44() method49Java | Mockito test45() method50Java | Mockito test46() method51Java | Mockito test47() method52Java | Mockito test48() method53Java | Mockito test49() method54Java | Mockito test50() method55Java | Mockito test51() method56Java | Mockito test52() method57Java | Mockito test53() method58Java | Mockito test54() method59Java | Mockito test55() method60Java | Mockito test56() method61Java | Mockito test57()

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful