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

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

Source:TestActiveStandbyElector.java Github

copy

Full Screen

...112 */113 @Test114 public void testJoinElection() {115 elector.joinElection(data);116 Mockito.verify(mockZK, Mockito.times(1)).create(ZK_LOCK_NAME, data,117 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);118 }119 /**120 * verify that successful znode create result becomes active and monitoring is121 * started122 */123 @Test124 public void testCreateNodeResultBecomeActive() throws Exception {125 mockNoPriorActive();126 127 elector.joinElection(data);128 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,129 ZK_LOCK_NAME);130 Mockito.verify(mockApp, Mockito.times(1)).becomeActive();131 verifyExistCall(1);132 // monitor callback verifies the leader is ephemeral owner of lock but does133 // not call becomeActive since its already active134 Stat stat = new Stat();135 stat.setEphemeralOwner(1L);136 Mockito.when(mockZK.getSessionId()).thenReturn(1L);137 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK, stat);138 // should not call neutral mode/standby/active139 Mockito.verify(mockApp, Mockito.times(0)).enterNeutralMode();140 Mockito.verify(mockApp, Mockito.times(0)).becomeStandby();141 Mockito.verify(mockApp, Mockito.times(1)).becomeActive();142 // another joinElection not called.143 Mockito.verify(mockZK, Mockito.times(1)).create(ZK_LOCK_NAME, data,144 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);145 // no new monitor called146 verifyExistCall(1);147 }148 149 /**150 * Verify that, when the callback fails to enter active state,151 * the elector rejoins the election after sleeping for a short period.152 */153 @Test154 public void testFailToBecomeActive() throws Exception {155 mockNoPriorActive();156 elector.joinElection(data);157 Assert.assertEquals(0, elector.sleptFor);158 159 Mockito.doThrow(new ServiceFailedException("failed to become active"))160 .when(mockApp).becomeActive();161 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,162 ZK_LOCK_NAME);163 // Should have tried to become active164 Mockito.verify(mockApp).becomeActive();165 166 // should re-join167 Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,168 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);169 Assert.assertEquals(2, count);170 Assert.assertTrue(elector.sleptFor > 0);171 }172 173 /**174 * Verify that, when the callback fails to enter active state, after175 * a ZK disconnect (i.e from the StatCallback), that the elector rejoins176 * the election after sleeping for a short period.177 */178 @Test179 public void testFailToBecomeActiveAfterZKDisconnect() throws Exception {180 mockNoPriorActive();181 elector.joinElection(data);182 Assert.assertEquals(0, elector.sleptFor);183 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,184 ZK_LOCK_NAME);185 Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,186 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);187 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,188 ZK_LOCK_NAME);189 verifyExistCall(1);190 Stat stat = new Stat();191 stat.setEphemeralOwner(1L);192 Mockito.when(mockZK.getSessionId()).thenReturn(1L);193 // Fake failure to become active from within the stat callback194 Mockito.doThrow(new ServiceFailedException("fail to become active"))195 .when(mockApp).becomeActive();196 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK, stat);197 Mockito.verify(mockApp, Mockito.times(1)).becomeActive();198 199 // should re-join200 Mockito.verify(mockZK, Mockito.times(3)).create(ZK_LOCK_NAME, data,201 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);202 Assert.assertEquals(2, count);203 Assert.assertTrue(elector.sleptFor > 0);204 }205 206 /**207 * Verify that, if there is a record of a prior active node, the208 * elector asks the application to fence it before becoming active.209 */210 @Test211 public void testFencesOldActive() throws Exception {212 byte[] fakeOldActiveData = new byte[0];213 mockPriorActive(fakeOldActiveData);214 215 elector.joinElection(data);216 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,217 ZK_LOCK_NAME);218 // Application fences active.219 Mockito.verify(mockApp, Mockito.times(1)).fenceOldActive(220 fakeOldActiveData);221 // Updates breadcrumb node to new data222 Mockito.verify(mockZK, Mockito.times(1)).setData(223 Mockito.eq(ZK_BREADCRUMB_NAME),224 Mockito.eq(data),225 Mockito.eq(0));226 // Then it becomes active itself227 Mockito.verify(mockApp, Mockito.times(1)).becomeActive();228 }229 230 @Test231 public void testQuitElectionRemovesBreadcrumbNode() throws Exception {232 mockNoPriorActive();233 elector.joinElection(data);234 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,235 ZK_LOCK_NAME);236 // Writes its own active info237 Mockito.verify(mockZK, Mockito.times(1)).create(238 Mockito.eq(ZK_BREADCRUMB_NAME), Mockito.eq(data),239 Mockito.eq(Ids.OPEN_ACL_UNSAFE),240 Mockito.eq(CreateMode.PERSISTENT));241 mockPriorActive(data);242 243 elector.quitElection(false);244 245 // Deletes its own active data246 Mockito.verify(mockZK, Mockito.times(1)).delete(247 Mockito.eq(ZK_BREADCRUMB_NAME), Mockito.eq(0));248 }249 /**250 * verify that znode create for existing node and no retry becomes standby and251 * monitoring is started252 */253 @Test254 public void testCreateNodeResultBecomeStandby() {255 elector.joinElection(data);256 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,257 ZK_LOCK_NAME);258 Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();259 verifyExistCall(1);260 }261 /**262 * verify that znode create error result in fatal error263 */264 @Test265 public void testCreateNodeResultError() {266 elector.joinElection(data);267 elector.processResult(Code.APIERROR.intValue(), ZK_LOCK_NAME, mockZK,268 ZK_LOCK_NAME);269 Mockito.verify(mockApp, Mockito.times(1)).notifyFatalError(270 "Received create error from Zookeeper. code:APIERROR " +271 "for path " + ZK_LOCK_NAME);272 }273 /**274 * verify that retry of network errors verifies master by session id and275 * becomes active if they match. monitoring is started.276 */277 @Test278 public void testCreateNodeResultRetryBecomeActive() throws Exception {279 mockNoPriorActive();280 281 elector.joinElection(data);282 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,283 ZK_LOCK_NAME);284 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,285 ZK_LOCK_NAME);286 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,287 ZK_LOCK_NAME);288 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,289 ZK_LOCK_NAME);290 // 4 errors results in fatalError291 Mockito292 .verify(mockApp, Mockito.times(1))293 .notifyFatalError(294 "Received create error from Zookeeper. code:CONNECTIONLOSS " +295 "for path " + ZK_LOCK_NAME + ". " +296 "Not retrying further znode create connection errors.");297 elector.joinElection(data);298 // recreate connection via getNewZooKeeper299 Assert.assertEquals(2, count);300 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,301 ZK_LOCK_NAME);302 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,303 ZK_LOCK_NAME);304 verifyExistCall(1);305 Stat stat = new Stat();306 stat.setEphemeralOwner(1L);307 Mockito.when(mockZK.getSessionId()).thenReturn(1L);308 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK, stat);309 Mockito.verify(mockApp, Mockito.times(1)).becomeActive();310 verifyExistCall(1);311 Mockito.verify(mockZK, Mockito.times(6)).create(ZK_LOCK_NAME, data,312 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);313 }314 /**315 * verify that retry of network errors verifies active by session id and316 * becomes standby if they dont match. monitoring is started.317 */318 @Test319 public void testCreateNodeResultRetryBecomeStandby() {320 elector.joinElection(data);321 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,322 ZK_LOCK_NAME);323 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,324 ZK_LOCK_NAME);325 verifyExistCall(1);326 Stat stat = new Stat();327 stat.setEphemeralOwner(0);328 Mockito.when(mockZK.getSessionId()).thenReturn(1L);329 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK, stat);330 Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();331 verifyExistCall(1);332 }333 /**334 * verify that if create znode results in nodeexists and that znode is deleted335 * before exists() watch is set then the return of the exists() method results336 * in attempt to re-create the znode and become active337 */338 @Test339 public void testCreateNodeResultRetryNoNode() {340 elector.joinElection(data);341 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,342 ZK_LOCK_NAME);343 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,344 ZK_LOCK_NAME);345 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,346 ZK_LOCK_NAME);347 verifyExistCall(1);348 elector.processResult(Code.NONODE.intValue(), ZK_LOCK_NAME, mockZK,349 (Stat) null);350 Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();351 Mockito.verify(mockZK, Mockito.times(4)).create(ZK_LOCK_NAME, data,352 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);353 }354 /**355 * verify that more than 3 network error retries result fatalError356 */357 @Test358 public void testStatNodeRetry() {359 elector.joinElection(data);360 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,361 (Stat) null);362 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,363 (Stat) null);364 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,365 (Stat) null);366 elector.processResult(Code.CONNECTIONLOSS.intValue(), ZK_LOCK_NAME, mockZK,367 (Stat) null);368 Mockito369 .verify(mockApp, Mockito.times(1))370 .notifyFatalError(371 "Received stat error from Zookeeper. code:CONNECTIONLOSS. "+372 "Not retrying further znode monitoring connection errors.");373 }374 /**375 * verify error in exists() callback results in fatal error376 */377 @Test378 public void testStatNodeError() {379 elector.joinElection(data);380 elector.processResult(Code.RUNTIMEINCONSISTENCY.intValue(), ZK_LOCK_NAME,381 mockZK, (Stat) null);382 Mockito.verify(mockApp, Mockito.times(0)).enterNeutralMode();383 Mockito.verify(mockApp, Mockito.times(1)).notifyFatalError(384 "Received stat error from Zookeeper. code:RUNTIMEINCONSISTENCY");385 }386 /**387 * verify behavior of watcher.process callback with non-node event388 */389 @Test390 public void testProcessCallbackEventNone() throws Exception {391 mockNoPriorActive();392 elector.joinElection(data);393 WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);394 Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.None);395 // first SyncConnected should not do anything396 Mockito.when(mockEvent.getState()).thenReturn(397 Event.KeeperState.SyncConnected);398 elector.processWatchEvent(mockZK, mockEvent);399 Mockito.verify(mockZK, Mockito.times(0)).exists(Mockito.anyString(),400 Mockito.anyBoolean(), Mockito.<AsyncCallback.StatCallback> anyObject(),401 Mockito.<Object> anyObject());402 // disconnection should enter safe mode403 Mockito.when(mockEvent.getState()).thenReturn(404 Event.KeeperState.Disconnected);405 elector.processWatchEvent(mockZK, mockEvent);406 Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();407 // re-connection should monitor master status408 Mockito.when(mockEvent.getState()).thenReturn(409 Event.KeeperState.SyncConnected);410 elector.processWatchEvent(mockZK, mockEvent);411 verifyExistCall(1);412 // session expired should enter safe mode and initiate re-election413 // re-election checked via checking re-creation of new zookeeper and414 // call to create lock znode415 Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.Expired);416 elector.processWatchEvent(mockZK, mockEvent);417 // already in safe mode above. should not enter safe mode again418 Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();419 // called getNewZooKeeper to create new session. first call was in420 // constructor421 Assert.assertEquals(2, count);422 // once in initial joinElection and one now423 Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,424 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);425 // create znode success. become master and monitor426 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,427 ZK_LOCK_NAME);428 Mockito.verify(mockApp, Mockito.times(1)).becomeActive();429 verifyExistCall(2);430 // error event results in fatal error431 Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.AuthFailed);432 elector.processWatchEvent(mockZK, mockEvent);433 Mockito.verify(mockApp, Mockito.times(1)).notifyFatalError(434 "Unexpected Zookeeper watch event state: AuthFailed");435 // only 1 state change callback is called at a time436 Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();437 }438 /**439 * verify behavior of watcher.process with node event440 */441 @Test442 public void testProcessCallbackEventNode() throws Exception {443 mockNoPriorActive();444 elector.joinElection(data);445 // make the object go into the monitoring state446 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,447 ZK_LOCK_NAME);448 Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();449 verifyExistCall(1);450 WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);451 Mockito.when(mockEvent.getPath()).thenReturn(ZK_LOCK_NAME);452 // monitoring should be setup again after event is received453 Mockito.when(mockEvent.getType()).thenReturn(454 Event.EventType.NodeDataChanged);455 elector.processWatchEvent(mockZK, mockEvent);456 verifyExistCall(2);457 // monitoring should be setup again after event is received458 Mockito.when(mockEvent.getType()).thenReturn(459 Event.EventType.NodeChildrenChanged);460 elector.processWatchEvent(mockZK, mockEvent);461 verifyExistCall(3);462 // lock node deletion when in standby mode should create znode again463 // successful znode creation enters active state and sets monitor464 Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeDeleted);465 elector.processWatchEvent(mockZK, mockEvent);466 // enterNeutralMode not called when app is standby and leader is lost467 Mockito.verify(mockApp, Mockito.times(0)).enterNeutralMode();468 // once in initial joinElection() and one now469 Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,470 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);471 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,472 ZK_LOCK_NAME);473 Mockito.verify(mockApp, Mockito.times(1)).becomeActive();474 verifyExistCall(4);475 // lock node deletion in active mode should enter neutral mode and create476 // znode again successful znode creation enters active state and sets477 // monitor478 Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeDeleted);479 elector.processWatchEvent(mockZK, mockEvent);480 Mockito.verify(mockApp, Mockito.times(1)).enterNeutralMode();481 // another joinElection called482 Mockito.verify(mockZK, Mockito.times(3)).create(ZK_LOCK_NAME, data,483 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);484 elector.processResult(Code.OK.intValue(), ZK_LOCK_NAME, mockZK,485 ZK_LOCK_NAME);486 Mockito.verify(mockApp, Mockito.times(2)).becomeActive();487 verifyExistCall(5);488 // bad path name results in fatal error489 Mockito.when(mockEvent.getPath()).thenReturn(null);490 elector.processWatchEvent(mockZK, mockEvent);491 Mockito.verify(mockApp, Mockito.times(1)).notifyFatalError(492 "Unexpected watch error from Zookeeper");493 // fatal error means no new connection other than one from constructor494 Assert.assertEquals(1, count);495 // no new watches after fatal error496 verifyExistCall(5);497 }498 private void verifyExistCall(int times) {499 Mockito.verify(mockZK, Mockito.times(times)).exists(500 Mockito.eq(ZK_LOCK_NAME), Mockito.<Watcher>any(),501 Mockito.same(elector),502 Mockito.same(mockZK));503 }504 /**505 * verify becomeStandby is not called if already in standby506 */507 @Test508 public void testSuccessiveStandbyCalls() {509 elector.joinElection(data);510 // make the object go into the monitoring standby state511 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,512 ZK_LOCK_NAME);513 Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();514 verifyExistCall(1);515 WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);516 Mockito.when(mockEvent.getPath()).thenReturn(ZK_LOCK_NAME);517 // notify node deletion518 // monitoring should be setup again after event is received519 Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.NodeDeleted);520 elector.processWatchEvent(mockZK, mockEvent);521 // is standby. no need to notify anything now522 Mockito.verify(mockApp, Mockito.times(0)).enterNeutralMode();523 // another joinElection called.524 Mockito.verify(mockZK, Mockito.times(2)).create(ZK_LOCK_NAME, data,525 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);526 // lost election527 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,528 ZK_LOCK_NAME);529 // still standby. so no need to notify again530 Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();531 // monitor is set again532 verifyExistCall(2);533 }534 /**535 * verify quit election terminates connection and there are no new watches.536 * next call to joinElection creates new connection and performs election537 */538 @Test539 public void testQuitElection() throws Exception {540 elector.joinElection(data);541 Mockito.verify(mockZK, Mockito.times(0)).close();542 elector.quitElection(true);543 Mockito.verify(mockZK, Mockito.times(1)).close();544 // no watches added545 verifyExistCall(0);546 byte[] data = new byte[8];547 elector.joinElection(data);548 // getNewZooKeeper called 2 times. once in constructor and once now549 Assert.assertEquals(2, count);550 elector.processResult(Code.NODEEXISTS.intValue(), ZK_LOCK_NAME, mockZK,551 ZK_LOCK_NAME);552 Mockito.verify(mockApp, Mockito.times(1)).becomeStandby();553 verifyExistCall(1);554 }555 /**556 * verify that receiveActiveData gives data when active exists, tells that557 * active does not exist and reports error in getting active information558 * 559 * @throws IOException560 * @throws InterruptedException561 * @throws KeeperException562 * @throws ActiveNotFoundException563 */564 @Test565 public void testGetActiveData() throws ActiveNotFoundException,566 KeeperException, InterruptedException, IOException {567 // get valid active data568 byte[] data = new byte[8];569 Mockito.when(570 mockZK.getData(Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false),571 Mockito.<Stat> anyObject())).thenReturn(data);572 Assert.assertEquals(data, elector.getActiveData());573 Mockito.verify(mockZK, Mockito.times(1)).getData(574 Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false),575 Mockito.<Stat> anyObject());576 // active does not exist577 Mockito.when(578 mockZK.getData(Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false),579 Mockito.<Stat> anyObject())).thenThrow(580 new KeeperException.NoNodeException());581 try {582 elector.getActiveData();583 Assert.fail("ActiveNotFoundException expected");584 } catch(ActiveNotFoundException e) {585 Mockito.verify(mockZK, Mockito.times(2)).getData(586 Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false),587 Mockito.<Stat> anyObject());588 }589 // error getting active data rethrows keeperexception590 try {591 Mockito.when(592 mockZK.getData(Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false),593 Mockito.<Stat> anyObject())).thenThrow(594 new KeeperException.AuthFailedException());595 elector.getActiveData();596 Assert.fail("KeeperException.AuthFailedException expected");597 } catch(KeeperException.AuthFailedException ke) {598 Mockito.verify(mockZK, Mockito.times(3)).getData(599 Mockito.eq(ZK_LOCK_NAME), Mockito.eq(false),600 Mockito.<Stat> anyObject());601 }602 }603 /**604 * Test that ensureBaseNode() recursively creates the specified dir605 */606 @Test607 public void testEnsureBaseNode() throws Exception {608 elector.ensureParentZNode();609 StringBuilder prefix = new StringBuilder();610 for (String part : ZK_PARENT_NAME.split("/")) {611 if (part.isEmpty()) continue;612 prefix.append("/").append(part);613 if (!"/".equals(prefix.toString())) {614 Mockito.verify(mockZK).create(615 Mockito.eq(prefix.toString()), Mockito.<byte[]>any(),616 Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT));617 }618 }619 }620 621 /**622 * Test for a bug encountered during development of HADOOP-8163:623 * ensureBaseNode() should throw an exception if it has to retry624 * more than 3 times to create any part of the path.625 */626 @Test627 public void testEnsureBaseNodeFails() throws Exception {628 Mockito.doThrow(new KeeperException.ConnectionLossException())629 .when(mockZK).create(630 Mockito.eq(ZK_PARENT_NAME), Mockito.<byte[]>any(),631 Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT));632 try {633 elector.ensureParentZNode();634 Assert.fail("Did not throw!");635 } catch (IOException ioe) {636 if (!(ioe.getCause() instanceof KeeperException.ConnectionLossException)) {637 throw ioe;638 }639 }640 // Should have tried three times641 Mockito.verify(mockZK, Mockito.times(3)).create(642 Mockito.eq(ZK_PARENT_NAME), Mockito.<byte[]>any(),643 Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT));644 }645 /**646 * verify the zookeeper connection establishment647 */648 @Test649 public void testWithoutZKServer() throws Exception {650 try {651 new ActiveStandbyElector("127.0.0.1", 2000, ZK_PARENT_NAME,652 Ids.OPEN_ACL_UNSAFE, Collections.<ZKAuthInfo> emptyList(), mockApp,653 CommonConfigurationKeys.HA_FC_ELECTOR_ZK_OP_RETRIES_DEFAULT);654 Assert.fail("Did not throw zookeeper connection loss exceptions!");655 } catch (KeeperException ke) {656 GenericTestUtils.assertExceptionContains( "ConnectionLoss", ke);657 }658 }659 /**660 * joinElection(..) should happen only after SERVICE_HEALTHY.661 */662 @Test663 public void testBecomeActiveBeforeServiceHealthy() throws Exception {664 mockNoPriorActive();665 WatchedEvent mockEvent = Mockito.mock(WatchedEvent.class);666 Mockito.when(mockEvent.getType()).thenReturn(Event.EventType.None);667 // session expired should enter safe mode668 // But for first time, before the SERVICE_HEALTY i.e. appData is set,669 // should not enter the election.670 Mockito.when(mockEvent.getState()).thenReturn(Event.KeeperState.Expired);671 elector.processWatchEvent(mockZK, mockEvent);672 // joinElection should not be called.673 Mockito.verify(mockZK, Mockito.times(0)).create(ZK_LOCK_NAME, null,674 Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL, elector, mockZK);675 }676}...

Full Screen

Full Screen

Source:TestCachingKeyProvider.java Github

copy

Full Screen

...32 Mockito.when(mockProv.getConf()).thenReturn(new Configuration());33 KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100);34 // asserting caching35 Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));36 Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1"));37 Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));38 Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1"));39 Thread.sleep(1200);40 Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));41 Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k1"));42 // asserting no caching when key is not known43 cache = new CachingKeyProvider(mockProv, 100, 100);44 Assert.assertEquals(null, cache.getCurrentKey("k2"));45 Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k2"));46 Assert.assertEquals(null, cache.getCurrentKey("k2"));47 Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k2"));48 }49 @Test50 public void testKeyVersion() throws Exception {51 KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class);52 KeyProvider mockProv = Mockito.mock(KeyProvider.class);53 Mockito.when(mockProv.getKeyVersion(Mockito.eq("k1@0")))54 .thenReturn(mockKey);55 Mockito.when(mockProv.getKeyVersion(Mockito.eq("k2@0"))).thenReturn(null);56 Mockito.when(mockProv.getConf()).thenReturn(new Configuration());57 KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100);58 // asserting caching59 Assert.assertEquals(mockKey, cache.getKeyVersion("k1@0"));60 Mockito.verify(mockProv, Mockito.times(1))61 .getKeyVersion(Mockito.eq("k1@0"));62 Assert.assertEquals(mockKey, cache.getKeyVersion("k1@0"));63 Mockito.verify(mockProv, Mockito.times(1))64 .getKeyVersion(Mockito.eq("k1@0"));65 Thread.sleep(200);66 Assert.assertEquals(mockKey, cache.getKeyVersion("k1@0"));67 Mockito.verify(mockProv, Mockito.times(2))68 .getKeyVersion(Mockito.eq("k1@0"));69 // asserting no caching when key is not known70 cache = new CachingKeyProvider(mockProv, 100, 100);71 Assert.assertEquals(null, cache.getKeyVersion("k2@0"));72 Mockito.verify(mockProv, Mockito.times(1))73 .getKeyVersion(Mockito.eq("k2@0"));74 Assert.assertEquals(null, cache.getKeyVersion("k2@0"));75 Mockito.verify(mockProv, Mockito.times(2))76 .getKeyVersion(Mockito.eq("k2@0"));77 }78 @Test79 public void testMetadata() throws Exception {80 KeyProvider.Metadata mockMeta = Mockito.mock(KeyProvider.Metadata.class);81 KeyProvider mockProv = Mockito.mock(KeyProvider.class);82 Mockito.when(mockProv.getMetadata(Mockito.eq("k1"))).thenReturn(mockMeta);83 Mockito.when(mockProv.getMetadata(Mockito.eq("k2"))).thenReturn(null);84 Mockito.when(mockProv.getConf()).thenReturn(new Configuration());85 KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100);86 // asserting caching87 Assert.assertEquals(mockMeta, cache.getMetadata("k1"));88 Mockito.verify(mockProv, Mockito.times(1)).getMetadata(Mockito.eq("k1"));89 Assert.assertEquals(mockMeta, cache.getMetadata("k1"));90 Mockito.verify(mockProv, Mockito.times(1)).getMetadata(Mockito.eq("k1"));91 Thread.sleep(200);92 Assert.assertEquals(mockMeta, cache.getMetadata("k1"));93 Mockito.verify(mockProv, Mockito.times(2)).getMetadata(Mockito.eq("k1"));94 // asserting no caching when key is not known95 cache = new CachingKeyProvider(mockProv, 100, 100);96 Assert.assertEquals(null, cache.getMetadata("k2"));97 Mockito.verify(mockProv, Mockito.times(1)).getMetadata(Mockito.eq("k2"));98 Assert.assertEquals(null, cache.getMetadata("k2"));99 Mockito.verify(mockProv, Mockito.times(2)).getMetadata(Mockito.eq("k2"));100 }101 @Test102 public void testRollNewVersion() throws Exception {103 KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class);104 KeyProvider mockProv = Mockito.mock(KeyProvider.class);105 Mockito.when(mockProv.getCurrentKey(Mockito.eq("k1"))).thenReturn(mockKey);106 Mockito.when(mockProv.getConf()).thenReturn(new Configuration());107 KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100);108 Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));109 Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1"));110 cache.rollNewVersion("k1");111 // asserting the cache is purged112 Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));113 Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k1"));114 cache.rollNewVersion("k1", new byte[0]);115 Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));116 Mockito.verify(mockProv, Mockito.times(3)).getCurrentKey(Mockito.eq("k1"));117 }118 @Test119 public void testDeleteKey() throws Exception {120 KeyProvider.KeyVersion mockKey = Mockito.mock(KeyProvider.KeyVersion.class);121 KeyProvider mockProv = Mockito.mock(KeyProvider.class);122 Mockito.when(mockProv.getCurrentKey(Mockito.eq("k1"))).thenReturn(mockKey);123 Mockito.when(mockProv.getKeyVersion(Mockito.eq("k1@0")))124 .thenReturn(mockKey);125 Mockito.when(mockProv.getMetadata(Mockito.eq("k1"))).thenReturn(126 new KMSClientProvider.KMSMetadata("c", 0, "l", null, new Date(), 1));127 Mockito.when(mockProv.getConf()).thenReturn(new Configuration());128 KeyProvider cache = new CachingKeyProvider(mockProv, 100, 100);129 Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));130 Mockito.verify(mockProv, Mockito.times(1)).getCurrentKey(Mockito.eq("k1"));131 Assert.assertEquals(mockKey, cache.getKeyVersion("k1@0"));132 Mockito.verify(mockProv, Mockito.times(1))133 .getKeyVersion(Mockito.eq("k1@0"));134 cache.deleteKey("k1");135 // asserting the cache is purged136 Assert.assertEquals(mockKey, cache.getCurrentKey("k1"));137 Mockito.verify(mockProv, Mockito.times(2)).getCurrentKey(Mockito.eq("k1"));138 Assert.assertEquals(mockKey, cache.getKeyVersion("k1@0"));139 Mockito.verify(mockProv, Mockito.times(2))140 .getKeyVersion(Mockito.eq("k1@0"));141 }142}...

Full Screen

Full Screen

Source:CreateContainerCmdHeaderTest.java Github

copy

Full Screen

...94 headers.put(DockerConstants.DOCKER_DNS, dns);95 headers.put(DockerConstants.DOCKER_DOMAIN_NAME, domainName);96 Mockito.when(mockObject.getHostConfig()).thenReturn(hostConfig);97 template.sendBodyAndHeaders("direct:in", "", headers);98 Mockito.verify(dockerClient, Mockito.times(1)).createContainerCmd(image);99 Mockito.verify(mockObject, Mockito.times(1)).withExposedPorts(eq(exposedPort));100 Mockito.verify(mockObject, Mockito.times(1)).withTty(eq(tty));101 Mockito.verify(mockObject, Mockito.times(1)).withName(eq(name));102 Mockito.verify(mockObject, Mockito.times(1)).withWorkingDir(workingDir);103 Mockito.verify(mockObject, Mockito.times(1)).withNetworkDisabled(disableNetwork);104 Mockito.verify(mockObject, Mockito.times(1)).withHostName(hostname);105 Mockito.verify(mockObject, Mockito.times(1)).withUser(user);106 Mockito.verify(mockObject, Mockito.times(1)).withStdinOpen(stdInOpen);107 Mockito.verify(mockObject, Mockito.times(1)).withStdInOnce(stdInOnce);108 Mockito.verify(mockObject, Mockito.times(1)).withAttachStderr(attachStdErr);109 Mockito.verify(mockObject, Mockito.times(1)).withAttachStdin(attachStdIn);110 Mockito.verify(mockObject, Mockito.times(1)).withAttachStdout(attachStdOut);111 Mockito.verify(mockObject, Mockito.times(1)).withVolumes(volumes);112 Mockito.verify(mockObject, Mockito.times(1)).withEnv(env);113 Mockito.verify(mockObject, Mockito.times(1)).withCmd(cmd);114 Mockito.verify(mockObject, Mockito.times(1)).withHostConfig(hostConfig);115 Mockito.verify(mockObject, Mockito.times(1)).withEntrypoint(entrypoint);116 Mockito.verify(mockObject, Mockito.times(1)).withPortSpecs(portSpecs);117 Mockito.verify(mockObject, Mockito.times(1)).withDomainName(domainName);118 Mockito.verify(hostConfig, Mockito.times(1)).withVolumesFrom(volumesFromContainer);119 Mockito.verify(hostConfig, Mockito.times(1)).withCapAdd(capAdd);120 Mockito.verify(hostConfig, Mockito.times(1)).withCapDrop(capDrop);121 Mockito.verify(hostConfig, Mockito.times(1)).withDns(dns);122 Mockito.verify(hostConfig, Mockito.times(1)).withMemory(memoryLimit);123 Mockito.verify(hostConfig, Mockito.times(1)).withMemorySwap(swapMemory);124 Mockito.verify(hostConfig, Mockito.times(1)).withCpuShares(cpuShares);125 }126 @Override127 protected void setupMocks() {128 Mockito.when(dockerClient.createContainerCmd(anyString())).thenReturn(mockObject);129 }130 @Override131 protected DockerOperation getOperation() {132 return DockerOperation.CREATE_CONTAINER;133 }134}...

Full Screen

Full Screen

Source:CommitContainerCmdHeaderTest.java Github

copy

Full Screen

...82 headers.put(DockerConstants.DOCKER_OPEN_STD_IN, openStdIn);83 headers.put(DockerConstants.DOCKER_STD_IN_ONCE, stdInOnce);84 headers.put(DockerConstants.DOCKER_PORT_SPECS, portSpecs);85 template.sendBodyAndHeaders("direct:in", "", headers);86 Mockito.verify(dockerClient, Mockito.times(1)).commitCmd(containerId);87 Mockito.verify(mockObject, Mockito.times(1)).withEnv(env);88 Mockito.verify(mockObject, Mockito.times(1)).withAttachStderr(attachStdErr);89 Mockito.verify(mockObject, Mockito.times(1)).withAttachStdin(attachStdIn);90 Mockito.verify(mockObject, Mockito.times(1)).withAttachStdout(attachStdOut);91 Mockito.verify(mockObject, Mockito.times(1)).withDisableNetwork(disableNetwork);92 Mockito.verify(mockObject, Mockito.times(1)).withTag(tag);93 Mockito.verify(mockObject, Mockito.times(1)).withRepository(repository);94 Mockito.verify(mockObject, Mockito.times(1)).withCmd(cmd);95 Mockito.verify(mockObject, Mockito.times(1)).withAuthor(author);96 Mockito.verify(mockObject, Mockito.times(1)).withMessage(message);97 Mockito.verify(mockObject, Mockito.times(1)).withPause(pause);98 Mockito.verify(mockObject, Mockito.times(1)).withExposedPorts(exposedPorts);99 Mockito.verify(mockObject, Mockito.times(1)).withMemory(memory);100 Mockito.verify(mockObject, Mockito.times(1)).withMemorySwap(swapMemory);101 Mockito.verify(mockObject, Mockito.times(1)).withWorkingDir(workingDir);102 Mockito.verify(mockObject, Mockito.times(1)).withUser(user);103 Mockito.verify(mockObject, Mockito.times(1)).withVolumes(volumes);104 Mockito.verify(mockObject, Mockito.times(1)).withTty(tty);105 Mockito.verify(mockObject, Mockito.times(1)).withHostname(hostname);106 Mockito.verify(mockObject, Mockito.times(1)).withOpenStdin(openStdIn);107 Mockito.verify(mockObject, Mockito.times(1)).withStdinOnce(stdInOnce);108 Mockito.verify(mockObject, Mockito.times(1)).withPortSpecs(portSpecs);109 }110 @Override111 protected void setupMocks() {112 Mockito.when(dockerClient.commitCmd(anyString())).thenReturn(mockObject);113 }114 @Override115 protected DockerOperation getOperation() {116 return DockerOperation.COMMIT_CONTAINER;117 }118}...

Full Screen

Full Screen

Source:DrawElementsTest.java Github

copy

Full Screen

...25 @Test26 public void drawFrogTest() {27 Frog frog = new Frog(1, 2);28 frog.draw(screen.newTextGraphics());29 Mockito.verify(graphics, Mockito.times(1)).setForegroundColor(TextColor.Factory.fromString("#33cc33"));30 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 2), "f");31 }32 @Test33 public void drawCarTest() {34 Car car = new Car(1, 2, "left");35 car.draw(screen.newTextGraphics());36 Mockito.verify(graphics, Mockito.times(1)).setForegroundColor(TextColor.Factory.fromString("#cb4335"));37 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 2), "c");38 }39 @Test40 public void drawTurtleTest() {41 Turtle turtle = new Turtle(1, 2, "left");42 turtle.draw(screen.newTextGraphics());43 Mockito.verify(graphics, Mockito.times(1)).setForegroundColor(TextColor.Factory.fromString("#e67e22"));44 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 2), "g");45 }46 @Test47 public void drawTreeTrunkTest() {48 TreeTrunk treeTrunk = new TreeTrunk(1, 2, "left");49 treeTrunk.draw(screen.newTextGraphics());50 Mockito.verify(graphics, Mockito.times(1)).setForegroundColor(TextColor.Factory.fromString("#7e5109"));51 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 2), "t");52 }53 @Test54 public void drawWaterTest() {55 Water water = new Water(1, 2);56 water.draw(screen.newTextGraphics());57 Mockito.verify(graphics, Mockito.times(2)).setForegroundColor(TextColor.Factory.fromString("#5dade2"));58 //Blocks in line y=159 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 1), String.valueOf(' '));60 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(2, 1), String.valueOf(' '));61 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(3, 1), String.valueOf(' '));62 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(4, 1), String.valueOf(' '));63 //Blocks in line y=264 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 2), String.valueOf(' '));65 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(2, 2), String.valueOf(' '));66 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(3, 2), String.valueOf(' '));67 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(4, 2), String.valueOf(' '));68 }69 @Test70 public void drawGrassTest() {71 Grass grass = new Grass(1, 2);72 grass.draw(screen.newTextGraphics());73 Mockito.verify(graphics, Mockito.times(2)).setForegroundColor(TextColor.Factory.fromString("#1e8449"));74 //Blocks in line y=175 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 1), String.valueOf(' '));76 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(2, 1), String.valueOf(' '));77 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(3, 1), String.valueOf(' '));78 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(4, 1), String.valueOf(' '));79 //Blocks in line y=280 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 2), String.valueOf(' '));81 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(2, 2), String.valueOf(' '));82 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(3, 2), String.valueOf(' '));83 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(4, 2), String.valueOf(' '));84 }85 @Test86 public void drawSideWalkTest() {87 Sidewalk sidewalk = new Sidewalk(1, 2);88 sidewalk.draw(screen.newTextGraphics());89 Mockito.verify(graphics, Mockito.times(2)).setForegroundColor(TextColor.Factory.fromString("#b175ff"));90 //Blocks in line y=191 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 1), String.valueOf(' '));92 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(2, 1), String.valueOf(' '));93 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(3, 1), String.valueOf(' '));94 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(4, 1), String.valueOf(' '));95 //Blocks in line y=296 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(1, 2), String.valueOf(' '));97 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(2, 2), String.valueOf(' '));98 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(3, 2), String.valueOf(' '));99 Mockito.verify(graphics, Mockito.times(1)).putString(new TerminalPosition(4, 2), String.valueOf(' '));100 }101}...

Full Screen

Full Screen

Source:HomepagePromoVariationManagerTest.java Github

copy

Full Screen

...4package org.chromium.chrome.browser.ntp.cards.promo;5import static org.mockito.ArgumentMatchers.any;6import static org.mockito.ArgumentMatchers.anyString;7import static org.mockito.Mockito.never;8import static org.mockito.Mockito.times;9import androidx.test.filters.SmallTest;10import org.junit.After;11import org.junit.Before;12import org.junit.Test;13import org.junit.runner.RunWith;14import org.mockito.Mock;15import org.mockito.Mockito;16import org.mockito.MockitoAnnotations;17import org.robolectric.annotation.Config;18import org.chromium.base.test.BaseRobolectricTestRunner;19import org.chromium.chrome.browser.flags.ChromeFeatureList;20/**21 * Unit test for {@link HomepagePromoVariationManager}.22 */23@RunWith(BaseRobolectricTestRunner.class)24@Config(manifest = Config.NONE)25public class HomepagePromoVariationManagerTest {26 private static final String STUDY_ENABLED_GROUP =27 ChromeFeatureList.HOMEPAGE_PROMO_SYNTHETIC_PROMO_SEEN_ENABLED;28 private static final String STUDY_TRACKING_GROUP =29 ChromeFeatureList.HOMEPAGE_PROMO_SYNTHETIC_PROMO_SEEN_TRACKING;30 @Mock31 public HomepagePromoVariationManager.SyntheticTrialHelper mStudyHelper;32 @Before33 public void setup() {34 MockitoAnnotations.initMocks(this);35 HomepagePromoVariationManager.getInstance().setStudyHelperForTests(mStudyHelper);36 }37 @After38 public void tearDown() {39 Mockito.reset(mStudyHelper);40 }41 @Test42 @SmallTest43 public void testStudyEnabled_EnabledGroup() {44 setStudyHelper(true, true, false);45 HomepagePromoVariationManager.getInstance().tagSyntheticHomepagePromoSeenGroup();46 Mockito.verify(mStudyHelper, times(1)).isClientInIPHTrackingOnlyGroup();47 Mockito.verify(mStudyHelper, times(1)).isClientInEnabledStudyGroup();48 Mockito.verify(mStudyHelper, never()).isClientInTrackingStudyGroup();49 Mockito.verify(mStudyHelper, never()).registerSyntheticFieldTrial(anyString());50 }51 @Test52 @SmallTest53 public void testStudyEnabled_TrackingGroup() {54 setStudyHelper(true, true, true);55 HomepagePromoVariationManager.getInstance().tagSyntheticHomepagePromoSeenGroup();56 Mockito.verify(mStudyHelper, times(1)).isClientInIPHTrackingOnlyGroup();57 Mockito.verify(mStudyHelper, never()).isClientInEnabledStudyGroup();58 Mockito.verify(mStudyHelper, times(1)).isClientInTrackingStudyGroup();59 Mockito.verify(mStudyHelper, never()).registerSyntheticFieldTrial(anyString());60 }61 /**62 * If study disabled and client in enabled group, STUDY_ENABLED_GROUP should be tagged in63 * SyntheticFieldTrial.64 */65 @Test66 @SmallTest67 public void testStudyDisabled_EnabledGroup() {68 setStudyHelper(true, false, false);69 HomepagePromoVariationManager.getInstance().tagSyntheticHomepagePromoSeenGroup();70 Mockito.verify(mStudyHelper, times(1)).isClientInIPHTrackingOnlyGroup();71 Mockito.verify(mStudyHelper, times(1)).isClientInEnabledStudyGroup();72 Mockito.verify(mStudyHelper, never()).isClientInTrackingStudyGroup();73 Mockito.verify(mStudyHelper, times(1)).registerSyntheticFieldTrial(STUDY_ENABLED_GROUP);74 Mockito.verify(mStudyHelper, never()).registerSyntheticFieldTrial(STUDY_TRACKING_GROUP);75 }76 /**77 * If study disabled and client in tracking-only, STUDY_TRACKING_GROUP should be tagged in78 * SyntheticFieldTrial.79 */80 @Test81 @SmallTest82 public void testStudyDisabled_TrackingGroup() {83 setStudyHelper(true, false, true);84 HomepagePromoVariationManager.getInstance().tagSyntheticHomepagePromoSeenGroup();85 Mockito.verify(mStudyHelper, times(1)).isClientInIPHTrackingOnlyGroup();86 Mockito.verify(mStudyHelper, never()).isClientInEnabledStudyGroup();87 Mockito.verify(mStudyHelper, times(1)).isClientInTrackingStudyGroup();88 Mockito.verify(mStudyHelper, never()).registerSyntheticFieldTrial(STUDY_ENABLED_GROUP);89 Mockito.verify(mStudyHelper, times(1)).registerSyntheticFieldTrial(STUDY_TRACKING_GROUP);90 }91 /**92 * If promo has never triggered, none of the study flag should be triggered, and93 * registerSyntheticFieldTrial should not happened.94 */95 @Test96 @SmallTest97 public void testNotEverTriggered() {98 setStudyHelper(false, true, true);99 HomepagePromoVariationManager.getInstance().tagSyntheticHomepagePromoSeenGroup();100 Mockito.verify(mStudyHelper, never()).isClientInIPHTrackingOnlyGroup();101 Mockito.verify(mStudyHelper, never()).isClientInEnabledStudyGroup();102 Mockito.verify(mStudyHelper, never()).isClientInTrackingStudyGroup();103 Mockito.verify(mStudyHelper, never()).registerSyntheticFieldTrial(any());...

Full Screen

Full Screen

Source:ProjectPersistenceTest.java Github

copy

Full Screen

...35 Mockito.when(mockedCriteria.list()).thenReturn(mockedResult);36 List<Project> result = this.persistence.list();37 Assert.assertNotNull(result);38 Assert.assertEquals(mockedResult, result);39 Mockito.verify(mockedSession,Mockito.times(1)).createCriteria(Project.class);40 Mockito.verify(mockedCriteria,Mockito.times(1)).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);41 Mockito.verify(mockedCriteria,Mockito.times(1)).list();42 Mockito.verify(mockedSession,Mockito.times(1)).close();43 }44 45 @Test46 public void testGet() throws ObjectNotFoundException{47 Project mockedResult = ProjectMO.build();48 Criteria mockedCriteria = Mockito.mock(Criteria.class);49 Mockito.when(mockedSession.createCriteria(Project.class)).thenReturn(mockedCriteria);50 Mockito.when(mockedCriteria.add(Mockito.any(Criterion.class))).thenReturn(mockedCriteria);51 Mockito.when(mockedCriteria.uniqueResult()).thenReturn(mockedResult);52 Project result = this.persistence.findById(ProjectMO.ID);53 Assert.assertNotNull(result);54 Assert.assertEquals(mockedResult, result);55 Mockito.verify(mockedSession,Mockito.times(1)).createCriteria(Project.class);56 Mockito.verify(mockedCriteria,Mockito.times(1)).add(Mockito.any(Criterion.class));57 Mockito.verify(mockedCriteria,Mockito.times(1)).uniqueResult();58 Mockito.verify(mockedSession,Mockito.times(1)).close();59 }60 61 @Test(expected = ObjectNotFoundException.class)62 public void testGetNull() throws ObjectNotFoundException{63 Criteria mockedCriteria = Mockito.mock(Criteria.class);64 Mockito.when(mockedSession.createCriteria(Project.class)).thenReturn(mockedCriteria);65 Mockito.when(mockedCriteria.add(Mockito.any(Criterion.class))).thenReturn(mockedCriteria);66 Mockito.when(mockedCriteria.uniqueResult()).thenReturn(null);67 Project result = this.persistence.findById(ProjectMO.ID);68 Assert.assertNull(result);69 Mockito.verify(mockedSession,Mockito.times(1)).createCriteria(Project.class);70 Mockito.verify(mockedCriteria,Mockito.times(1)).add(Mockito.any(Criterion.class));71 Mockito.verify(mockedCriteria,Mockito.times(1)).uniqueResult();72 Mockito.verify(mockedSession,Mockito.times(1)).close();73 }74 75 @Test76 public void testSave(){77 Project mockedProject = ProjectMO.build();78 this.persistence.create(mockedProject);79 Mockito.verify(mockedSession, Mockito.times(1)).save(mockedProject);80 Mockito.verify(mockedSession, Mockito.times(1)).flush();81 Mockito.verify(mockedSession, Mockito.times(1)).close();82 }83}...

Full Screen

Full Screen

Source:CustomerControllerTest.java Github

copy

Full Screen

...26 final Customer created = new Customer(F_NAME, L_NAME);27 Mockito.when(utils.getString()).thenReturn(F_NAME, L_NAME);28 Mockito.when(dao.create(created)).thenReturn(created);29 assertEquals(created, controller.create());30 Mockito.verify(utils, Mockito.times(2)).getString();31 Mockito.verify(dao, Mockito.times(1)).create(created);32 }33 @Test34 public void testReadAll() {35 List<Customer> customers = new ArrayList<>();36 customers.add(new Customer(1L, "jordan", "harrison"));37 Mockito.when(dao.readAll()).thenReturn(customers);38 assertEquals(customers, controller.readAll());39 Mockito.verify(dao, Mockito.times(1)).readAll();40 }41 @Test42 public void testUpdate() {43 Customer updated = new Customer(1L, "chris", "perrins");44 Mockito.when(this.utils.getLong()).thenReturn(1L);45 Mockito.when(this.utils.getString()).thenReturn(updated.getFirstName(), updated.getSurname());46 Mockito.when(this.dao.update(updated)).thenReturn(updated);47 assertEquals(updated, this.controller.update());48 Mockito.verify(this.utils, Mockito.times(1)).getLong();49 Mockito.verify(this.utils, Mockito.times(2)).getString();50 Mockito.verify(this.dao, Mockito.times(1)).update(updated);51 }52 @Test53 public void testDelete() {54 final long ID = 1L;55 Mockito.when(utils.getLong()).thenReturn(ID);56 Mockito.when(dao.delete(ID)).thenReturn(1);57 assertEquals(1L, this.controller.delete());58 Mockito.verify(utils, Mockito.times(1)).getLong();59 Mockito.verify(dao, Mockito.times(1)).delete(ID);60 }61}...

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1import static org.mockito.Mockito.times;2import static org.mockito.Mockito.when;3import static org.mockito.Mockito.verify;4import static org.mockito.Mockito.mock;5import static org.mockito.Matchers.anyString;6import static org.mockito.Matchers.anyInt;7import static org.mockito.Matchers.anyObject;8import static org.mockito.Matchers.any;9import static org.mockito.Matchers.eq;10import static org.mockito.Mockito.verifyNoMoreInteractions;11import static org.mockito.Mockito.verifyZeroInteractions;12import static org.mockito.Mockito.doThrow;13import static org.mockito.Mockito.doReturn;14import static org.mockito.Mockito.doNothing;15import static org.mockito.Mockito.doCallRealMethod;16import static org.mockito.Mockito.inOrder;17import static org.mockito.Mockito.spy;18import static org.mockito.Mockito.reset;19import static org.mockito.Mockito.mockStatic;20import static org.mockito.Mockito.verifyStatic;21import static org.mockito.Mockito.verifyNoMoreInteractions;22import static org.mockito.Mockito.verifyZeroInteractions;23import static org.mockito.Mockito.doThrow;24import static org.mockito.Mockito.do

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1package org.kodejava.example.mockito;2import org.junit.Test;3import org.junit.runner.RunWith;4import org.mockito.Mock;5import org.mockito.runners.MockitoJUnitRunner;6import java.util.List;7import static org.mockito.Mockito.times;8import static org.mockito.Mockito.verify;9@RunWith(MockitoJUnitRunner.class)10public class TimesTest {11 private List<String> list;12 public void testTimes() {13 list.add("One");14 list.add("Two");15 list.add("Three");16 verify(list, times(3)).add("One");17 verify(list, times(3)).add("Two");18 verify(list, times(3)).add("Three");19 }20}21Share on Skype (Opens in new window)

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1package com.ack.j2se.mockito;2import org.junit.Test;3import org.mockito.Mockito;4import java.util.ArrayList;5import java.util.List;6import static org.junit.Assert.assertEquals;7import static org.mockito.Mockito.times;8import static org.mockito.Mockito.when;9public class MockitoTimesTest {10 public void testTimes() {11 List mockedList = Mockito.mock( ArrayList.class );12 mockedList.add( "one" );13 mockedList.clear();14 Mockito.verify( mockedList ).add( "one" );15 Mockito.verify( mockedList ).clear();16 Mockito.verify( mockedList, times( 2 ) ).clear();17 Mockito.verify( mockedList, times( 3 ) ).add( "one" );18 Mockito.verify( mockedList, times( 3 ) ).add( "one" );19 }20 public void testTimesInStubbing() {21 ArrayList mockedList = Mockito.mock( ArrayList.class );22 when( mockedList.get( 0 ) ).thenReturn( "first" );23 when( mockedList.get( 1 ) ).thenThrow( new RuntimeException() );24 System.out.println( mockedList.get( 0 ) );25 System.out.println( mockedList.get( 1 ) );26 System.out.println( mockedList.get( 999 ) );

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1package org.kodejava.example.mockito;2import org.junit.Test;3import org.mockito.Mockito;4import java.util.List;5public class TimesExample {6 public void testTimes() {7 List list = Mockito.mock(List.class);8 list.add("one");9 list.add("two");10 list.add("three");11 Mockito.verify(list, Mockito.times(3)).add(Mockito.anyString());12 }13}

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2import static org.mockito.Mockito.times;3import org.mockito.invocation.InvocationOnMock;4import org.mockito.stubbing.Answer;5import org.junit.Test;6import static org.junit.Assert.*;7import org.mockito.ArgumentCaptor;8import org.mockito.InOrder;9import org.mockito.Mock;10import org.mockito.MockitoAnnotations;11import org.mockito.Spy;12import org.mockito.exceptions.verification.NoInteractionsWanted;13import org.mockito.exceptions.verification.TooLittleActualInvocations;14import org.mockito.exceptions.verification.WantedButNotInvoked;15import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;16import org.mockito.exceptions.verification.junit.NeverWantedButInvoked;17import org.mockito.exceptions.verification.junit.TooLittleActualInvocations;18import org.mockito.exceptions.verification.junit.TooManyActualInvocations;19import org.mockito.exceptions.verification.junit.UnfinishedVerificationException;20import org.mockito.exceptions.verification.junit.WantedButNotInvoked;21import org.mockito.exceptions.verification.junit.WantedButNotInvokedInOrder;22import org.mockito.internal.util.MockUtil;23import org.mockito.internal.util.MockUtil;24import org.mockito.invocation.Invocation;25import org.mockito.invocation.InvocationOnMock;26import org.mockito.stubbing.Answer;27import org.mockito.stubbing.OngoingStubbing;28import org.mockito.verification.VerificationMode;29import org.mockit

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2import static org.mockito.Mockito.*;3public class 1 {4 public static void main(String[] args) {5 List mockedList = mock(List.class);6 mockedList.add("one");7 mockedList.add("two");8 verify(mockedList, times(1)).add("one");9 verify(mockedList, times(2)).add("two");10 verify(mockedList, times(0)).add("three");11 }12}13list.add("one");14-> at 1.main(1.java:14)15-> at 1.main(1.java:14)16list.add("two");17-> at 1.main(1.java:15)18-> at 1.main(1.java:15)19-> at 1.main(1.java:16)20-> at 1.main(1.java:16)21import org.mockito.Mockito;22import static org.mockito.Mockito.*;23public class 2 {24 public static void main(String[] args) {25 List mockedList = mock(List.class);26 mockedList.add("one");27 mockedList.add("two");28 mockedList.add("three");29 verify(mockedList, atLeast(1)).add("one");30 verify(mockedList, atLeast(2)).add("two");31 verify(mockedList, atLeast(3

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2public class 1 {3 public static void main(String[] args) {4 ArrayList mockList = Mockito.mock(ArrayList.class);5 Mockito.when(mockList.add("one")).thenReturn(true);6 mockList.add("one");7 mockList.add("one");8 mockList.add("one");9 Mockito.verify(mockList, Mockito.times(3)).add("one");10 }11}12list.add("one");13-> at 1.main(1.java:11)14-> at 1.main(1.java:9)15Related Posts: Mockito verify() method with times16Mockito verify() method with never17Mockito verify() method with atLeastOnce18Mockito verify() method with atLeast19Mockito verify() method with atMost20Mockito verify() method with after21Mockito verify() method with before22Mockito verify()

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1package org.letuslearn.mockito;2import java.util.List;3import org.junit.Test;4import org.mockito.Mockito;5public class MockitoTimesMethod {6public void test() {7List mockedList = Mockito.mock(List.class);8mockedList.add("one");9mockedList.add("two");10mockedList.add("three");11mockedList.add("four");12Mockito.verify(mockedList).add("one");13Mockito.verify(mockedList).add("two");14Mockito.verify(mockedList).add("three");15Mockito.verify(mockedList).add("four");16Mockito.verify(mockedList, Mockito.times(4)).add(Mockito.anyString());17}18}19-> at org.letuslearn.mockito.MockitoTimesMethod.test(MockitoTimesMethod.java:17)20-> at org.letuslearn.mockito.MockitoTimesMethod.test(MockitoTimesMethod.java:17)21at org.letuslearn.mockito.MockitoTimesMethod.test(MockitoTimesMethod.java:21)

Full Screen

Full Screen

times

Using AI Code Generation

copy

Full Screen

1import org.mockito.Mockito;2public class Example {3 public static void main(String args[]) {4 Example ex = new Example();5 ex.timesExample();6 }7 public void timesExample() {8 Example mock = Mockito.mock(Example.class);9 Mockito.when(mock.getDetails()).thenReturn("mock details").times(3);10 System.out.println(mock.getDetails());11 System.out.println(mock.getDetails());12 System.out.println(mock.getDetails());13 System.out.println(mock.getDetails());14 }15 public String getDetails() {16 return "original details";17 }18}19Example 2: times() method with verify() method20import org.mockito.Mockito;21public class Example {22 public static void main(String args[]) {23 Example ex = new Example();24 ex.timesExample();25 }26 public void timesExample() {27 Example mock = Mockito.mock(Example.class);28 Mockito.when(mock.getDetails()).thenReturn("mock details").times(2);29 System.out.println(mock.getDetails());30 System.out.println(mock.getDetails());31 System.out.println(mock.getDetails());32 Mockito.verify(mock, Mockito.times(2)).getDetails();33 }34 public String getDetails() {35 return "original details";36 }37}38Example 3: times() method with verify() method39import org.mockito.Mockito;40public class Example {41 public static void main(String args[]) {42 Example ex = new Example();43 ex.timesExample();44 }45 public void timesExample() {46 Example mock = Mockito.mock(Example.class);47 Mockito.when(mock.getDetails()).thenReturn("mock details").times(4);48 System.out.println(mock.getDetails());49 System.out.println(mock.getDetails());50 System.out.println(mock.getDetails());

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