@Override public Stubber doNothing() { return stubber().doNothing(); }
public BDDStubber willDoNothing() { return new BDDStubberImpl(mockitoStubber.doNothing()); }
public BDDStubber willNothing() { return new BDDStubberImpl(mockitoStubber.doNothing()); }
return MOCKITO_CORE.stubber().doNothing();
@Test public void testWhenFailureDuringInit() throws InterruptedException { when(tokenStore.fetchSegments(anyString())) .thenThrow(new RuntimeException("Faking issue during fetchSegments")) .thenReturn(new int[]{}) .thenReturn(new int[]{0}); doThrow(new RuntimeException("Faking issue during initializeTokenSegments")) // and on further calls .doNothing() .when(tokenStore).initializeTokenSegments(anyString(), anyInt()); testSubject.start(); Thread.sleep(2500); assertEquals(1, testSubject.activeProcessorThreads()); }
@Test public void testRetryInit() throws MetaException { IHMSHandler mockBaseHandler = Mockito.mock(IHMSHandler.class); Mockito.when(mockBaseHandler.getConf()).thenReturn(conf); Mockito .doThrow(JDOException.class) .doNothing() .when(mockBaseHandler).init(); RetryingHMSHandler.getProxy(conf, mockBaseHandler, false); Mockito.verify(mockBaseHandler, Mockito.times(2)).init(); }
@Test public void put_should_return_when_underlying_put_does_not_throw_test() throws Exception { OffHeapChainMap<Long> offheapChainMap = getOffHeapChainMapLongMock(); ChainStorageEngine<Long> storageEngine = getChainStorageEngineLongMock(); when(offheapChainMap.getStorageEngine()).thenReturn(storageEngine); when(offheapChainMap.writeLock()).thenReturn(new ReentrantLock()); // throw once, then ok doThrow(new OversizeMappingException()) .doNothing() .when(offheapChainMap).put(anyLong(), any(Chain.class)); OffHeapServerStore offHeapServerStore = new OffHeapServerStore(singletonList(offheapChainMap), mock(KeySegmentMapper.class)); offHeapServerStore.put(43L, mock(Chain.class)); }
@Test public void testWrappedMetaExceptionRetry() throws MetaException { IHMSHandler mockBaseHandler = Mockito.mock(IHMSHandler.class); Mockito.when(mockBaseHandler.getConf()).thenReturn(conf); //JDOException wrapped in MetaException wrapped in InvocationException MetaException me = new MetaException("Dummy exception"); me.initCause(new JDOException()); InvocationTargetException ex = new InvocationTargetException(me); Mockito .doThrow(me) .doNothing() .when(mockBaseHandler).init(); RetryingHMSHandler.getProxy(conf, mockBaseHandler, false); Mockito.verify(mockBaseHandler, Mockito.times(2)).init(); } }
private void executeFailsWithExceptionThrownByInterrupter() { Thread currentThread = Thread.currentThread(); ComputationStepExecutor underTest = new ComputationStepExecutor(mockComputationSteps(computationStep1, computationStep2, computationStep3), taskInterrupter); RuntimeException exception = new RuntimeException("mocking fail of method check()"); doNothing() .doNothing() .doThrow(exception) .when(taskInterrupter) .check(currentThread); try { underTest.execute(); fail("execute should have thrown an exception"); } catch (Exception e) { assertThat(e).isSameAs(exception); } }
@Test public void commitAbort() { final SpannerException error = SpannerExceptionFactory.newSpannerException( SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, "")); doThrow(error).doNothing().when(txn).commit(); long backoffMillis = 100L; when(txn.getRetryDelayInMillis(any(BackOff.class))).thenReturn(backoffMillis); final AtomicInteger numCalls = new AtomicInteger(0); transactionRunner.run( new TransactionCallable<Void>() { @Override public Void run(TransactionContext transaction) throws Exception { numCalls.incrementAndGet(); return null; } }); assertThat(numCalls.get()).isEqualTo(2); verify(sleeper, times(1)).backoffSleep(any(Context.class), eq(backoffMillis)); verify(txn, times(2)).ensureTxn(); }
@Test public void execute_void() throws Exception { final DevNullService.Iface client = new ClientBuilder(server.uri(BINARY, "/thrift-devnull")) .rpcDecorator(RetryingRpcClient.newDecorator(retryOnException, 10)) .build(DevNullService.Iface.class); doThrow(new IllegalArgumentException()) .doThrow(new IllegalArgumentException()) .doNothing() .when(devNullServiceHandler).consume(anyString()); client.consume("hello"); verify(devNullServiceHandler, times(3)).consume("hello"); }
@Test public void updatesMembershipViewFromFirstNonPersistentReplicateThatReplies() { persistenceInitialImageAdvisor = persistenceInitialImageAdvisorWithDiskImage(); InitialImageAdvice adviceWithNonPersistentReplicates = adviceWithNonPersistentReplicates(4); when(cacheDistributionAdvisor.adviseInitialImage(isNull(), anyBoolean())) .thenReturn(adviceWithNonPersistentReplicates, adviceWithReplicates(1)); doThrow(ReplyException.class) // Throw on first call .doNothing() // Then return without throwing .when(persistenceAdvisor).updateMembershipView(any(), anyBoolean()); persistenceInitialImageAdvisor.getAdvice(null); // The second call succeeds. Expect no further calls. verify(persistenceAdvisor, times(2)).updateMembershipView(any(), anyBoolean()); }
@Test public void put_should_return_when_underlying_put_does_not_throw_with_keymapper_test() throws Exception { long theKey = 43L; ChainStorageEngine<Long> storageEngine = getChainStorageEngineLongMock(); OffHeapChainMap<Long> offheapChainMap = getOffHeapChainMapLongMock(); OffHeapChainMap<Long> otherOffheapChainMap = getOffHeapChainMapLongMock(); when(offheapChainMap.shrink()).thenReturn(true); when(offheapChainMap.getStorageEngine()).thenReturn(storageEngine); when(offheapChainMap.writeLock()).thenReturn(new ReentrantLock()); when(otherOffheapChainMap.writeLock()).thenReturn(new ReentrantLock()); // throw twice, then OK doThrow(new OversizeMappingException()) .doThrow(new OversizeMappingException()) .doNothing() .when(otherOffheapChainMap).put(anyLong(), any(Chain.class)); KeySegmentMapper keySegmentMapper = mock(KeySegmentMapper.class); when(keySegmentMapper.getSegmentForKey(theKey)).thenReturn(1); OffHeapServerStore offHeapServerStore = new OffHeapServerStore(asList(offheapChainMap, otherOffheapChainMap), keySegmentMapper); offHeapServerStore.put(theKey, mock(Chain.class)); //getSegmentForKey was called 4 times : segmentFor, handleOversizeMappingException, segmentFor, segmentFor verify(keySegmentMapper, times(4)).getSegmentForKey(theKey); }
@Override public StaticCapableStubber doNothing() { instanceStubber = instanceStubber.doNothing(); return this; }
@Test public void failWithConnectionExceptionWhenStartingAndGetsReconnected() throws Exception { doThrow(new RuntimeException(new ConnectionException(ERROR_MESSAGE))) .doThrow(new RuntimeException(new ConnectionException(ERROR_MESSAGE))).doNothing().when(source).onStart(sourceCallback); messageSource.initialise(); messageSource.start(); verify(source, times(3)).onStart(sourceCallback); verify(source, times(2)).onStop(); }
@Test public void call_rm_and_retry_up_if_conflicting_containers_exist() throws IOException, InterruptedException { String conflictingContainer = "conflictingContainer"; doThrow(new DockerExecutionException("The name \"" + conflictingContainer + "\" is already in use")) .doNothing() .when(dockerCompose).up(); ConflictingContainerRemovingDockerCompose conflictingContainerRemovingDockerCompose = new ConflictingContainerRemovingDockerCompose(dockerCompose, docker); conflictingContainerRemovingDockerCompose.up(); verify(dockerCompose, times(2)).up(); verify(docker).rm(ImmutableSet.of(conflictingContainer)); }
@Test public void ignore_docker_execution_exceptions_in_rm() throws IOException, InterruptedException { String conflictingContainer = "conflictingContainer"; doThrow(new DockerExecutionException("The name \"" + conflictingContainer + "\" is already in use")) .doNothing() .when(dockerCompose).up(); doThrow(DockerExecutionException.class).when(docker).rm(anySetOf(String.class)); ConflictingContainerRemovingDockerCompose conflictingContainerRemovingDockerCompose = new ConflictingContainerRemovingDockerCompose(dockerCompose, docker); conflictingContainerRemovingDockerCompose.up(); verify(dockerCompose, times(2)).up(); verify(docker).rm(ImmutableSet.of(conflictingContainer)); }
@Test public void fail_on_non_docker_execution_exceptions_in_rm() throws IOException, InterruptedException { String conflictingContainer = "conflictingContainer"; doThrow(new DockerExecutionException("The name \"" + conflictingContainer + "\" is already in use")) .doNothing() .when(dockerCompose).up(); doThrow(RuntimeException.class).when(docker).rm(anySetOf(String.class)); exception.expect(RuntimeException.class); ConflictingContainerRemovingDockerCompose conflictingContainerRemovingDockerCompose = new ConflictingContainerRemovingDockerCompose(dockerCompose, docker); conflictingContainerRemovingDockerCompose.up(); }