@Override public Stubber doThrow(Class<? extends Throwable> toBeThrown) { return stubber().doThrow(toBeThrown); }
@Override public Stubber doThrow(Throwable... toBeThrown) { return stubber().doThrow(toBeThrown); }
@Override public Stubber doThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) { return stubber().doThrow(toBeThrown, nextToBeThrown); }
public BDDStubber willThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) { return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown, nextToBeThrown)); }
public BDDStubber willThrow(Throwable... toBeThrown) { return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown)); }
public BDDStubber willThrow(Class<? extends Throwable> toBeThrown) { return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown)); }
public BDDStubber willThrow(Throwable toBeThrown) { return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown)); }
public BDDStubber willThrow(Class<? extends Throwable> toBeThrown) { return new BDDStubberImpl(mockitoStubber.doThrow(toBeThrown)); }
/** * Use <code>doThrow()</code> when you want to stub the void method with an exception. * <p> * Stubbing voids requires different approach from {@link Mockito#when(Object)} because the compiler * does not like void methods inside brackets... * <p> * Example: * * <pre class="code"><code class="java"> * doThrow(new RuntimeException()).when(mock).someVoidMethod(); * </code></pre> * * @param toBeThrown to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing */ @CheckReturnValue public static Stubber doThrow(Throwable... toBeThrown) { return MOCKITO_CORE.stubber().doThrow(toBeThrown); }
/** * Use <code>doThrow()</code> when you want to stub the void method with an exception. * <p> * A new exception instance will be created for each method invocation. * <p> * Stubbing voids requires different approach from {@link Mockito#when(Object)} because the compiler * does not like void methods inside brackets... * <p> * Example: * * <pre class="code"><code class="java"> * doThrow(RuntimeException.class).when(mock).someVoidMethod(); * </code></pre> * * @param toBeThrown to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing * @since 2.1.0 */ @CheckReturnValue public static Stubber doThrow(Class<? extends Throwable> toBeThrown) { return MOCKITO_CORE.stubber().doThrow(toBeThrown); }
/** * Same as {@link #doThrow(Class)} but sets consecutive exception classes to be thrown. Remember to use * <code>doThrow()</code> when you want to stub the void method to throw several exception of specified class. * <p> * A new exception instance will be created for each method invocation. * <p> * Stubbing voids requires different approach from {@link Mockito#when(Object)} because the compiler * does not like void methods inside brackets... * <p> * Example: * * <pre class="code"><code class="java"> * doThrow(RuntimeException.class, BigFailure.class).when(mock).someVoidMethod(); * </code></pre> * * @param toBeThrown to be thrown when the stubbed method is called * @param toBeThrownNext next to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing * @since 2.1.0 */ // Additional method helps users of JDK7+ to hide heap pollution / unchecked generics array creation @SuppressWarnings ({"unchecked", "varargs"}) @CheckReturnValue public static Stubber doThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... toBeThrownNext) { return MOCKITO_CORE.stubber().doThrow(toBeThrown, toBeThrownNext); }
@Override public Stubber doThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) { Stubber stubber = doThrow(toBeThrown); if (nextToBeThrown == null) { mockingProgress().reset(); throw notAnException(); } for (Class<? extends Throwable> next : nextToBeThrown) { stubber = stubber.doThrow(next); } return stubber; }
private static DbVersion newMockFailingOnSecondBuildCall() { DbVersion res = mock(DbVersion.class); doNothing() .doThrow(new RuntimeException("addStep should not be called twice")) .when(res) .addSteps(any(MigrationStepRegistry.class)); return res; } }
@Test public void shouldThrowIfCannotDeleteCommandTopic() { // Given: givenTopicsExistInKafka("_confluent-ksql-command_topic_command_topic"); doThrow(KsqlException.class) .doThrow(KsqlException.class) .doThrow(KsqlException.class) .doThrow(KsqlException.class) .doThrow(KsqlException.class) .when(kafkaTopicClient) .deleteTopics(Collections.singletonList("_confluent-ksql-command_topic_command_topic")); expectedException.expect(KsqlException.class); expectedException.expectMessage( "Exception while deleting topics: _confluent-ksql-command_topic_command_topic"); // When: clusterTerminator.terminateCluster(Collections.emptyList()); }
@Test public void shouldThrowIfCouldNotDeleteTopicListWithPattern() { // Given: givenTopicsExistInKafka("K_Foo"); givenSinkTopicsExistInMetastore("K_Foo"); doThrow(KsqlException.class) .doThrow(KsqlException.class) .doThrow(KsqlException.class) .doThrow(KsqlException.class) .doThrow(KsqlException.class) .when(kafkaTopicClient).deleteTopics(Collections.singletonList("K_Foo")); expectedException.expect(KsqlException.class); expectedException.expectMessage("Exception while deleting topics: K_Foo"); // When: clusterTerminator.terminateCluster(ImmutableList.of("K_Fo*")); }
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 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 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); }
@Test public void projectLifeCycleListeners_onProjectsDeleted_called_even_if_delete_fails() { userSession.logIn().addPermission(ADMINISTER, org1); ComponentDto project1 = db.components().insertPrivateProject(org1); ComponentDto project2 = db.components().insertPrivateProject(org1); ComponentDto project3 = db.components().insertPrivateProject(org1); ComponentCleanerService componentCleanerService = mock(ComponentCleanerService.class); RuntimeException expectedException = new RuntimeException("Faking delete failing on 2nd project"); doNothing() .doThrow(expectedException) .when(componentCleanerService) .delete(any(), any(ComponentDto.class)); try { ws.newRequest() .setParam("organization", org1.getKey()) .setParam("projects", project1.getDbKey() + "," + project2.getDbKey() + "," + project3.getDbKey()) .execute(); } catch (RuntimeException e) { assertThat(e).isSameAs(expectedException); verifyListenersOnProjectsDeleted(project1, project2, project3); } }
@Test public void getProperties_return_properties_from_previous_thread_cache_if_DB_error_on_not_first_call() { String key = randomAlphanumeric(3); String value1 = randomAlphanumeric(4); String value2 = randomAlphanumeric(5); SettingLoader settingLoaderMock = mock(SettingLoader.class); PersistenceException toBeThrown = new PersistenceException("Faking an error connecting to DB"); doAnswer(invocationOnMock -> ImmutableMap.of(key, value1)) .doThrow(toBeThrown) .doAnswer(invocationOnMock -> ImmutableMap.of(key, value2)) .when(settingLoaderMock) .loadAll(); underTest = new ThreadLocalSettings(new PropertyDefinitions(), new Properties(), settingLoaderMock); underTest.load(); assertThat(underTest.getProperties()) .containsOnly(entry(key, value1)); underTest.unload(); underTest.load(); assertThat(underTest.getProperties()) .containsOnly(entry(key, value1)); underTest.unload(); underTest.load(); assertThat(underTest.getProperties()) .containsOnly(entry(key, value2)); underTest.unload(); }