@Override public Stubber doThrow(Throwable... toBeThrown) { if (toBeThrown == null) { answers.add(new ThrowsException(null)); return this; } for (Throwable throwable : toBeThrown) { answers.add(new ThrowsException(throwable)); } return this; }
public Stubber doThrow(Throwable toBeThrown) { answers.add(new ThrowsException(toBeThrown)); return this; }
private OngoingStubbing<T> thenThrow(Throwable throwable) { return thenAnswer(new ThrowsException(throwable)); }
public VoidMethodStubbable<T> toThrow(Throwable throwable) { invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(throwable)); return this; }
public DeprecatedOngoingStubbing<T> toThrow(Throwable throwable) { return toAnswer(new ThrowsException(throwable)); } }
private OngoingStubbing<T> thenThrow(Throwable throwable) { return thenAnswer(new ThrowsException(throwable)); }
/** * 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 */ public static Stubber doThrow(Throwable toBeThrown) { return MOCKITO_CORE.doAnswer(new ThrowsException(toBeThrown)); }
/** * Use doThrow() 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> * doThrow(new RuntimeException()).when(mock).someVoidMethod(); * </pre> * * @param toBeThrown * to be thrown when the stubbed method is called * @return stubber - to select a method for stubbing */ public static PowerMockitoStubber doThrow(Throwable toBeThrown) { return POWERMOCKITO_CORE.doAnswer(new ThrowsException(toBeThrown)); }
@Test public void shouldAbbreviateWellKnownIOErrors() { // given OutputStream output = mock( OutputStream.class, new ThrowsException( new IOException( "Broken pipe" ) ) ); AssertableLogProvider logProvider = new AssertableLogProvider(); ExecutionResultSerializer serializer = getSerializerWith( output, null, logProvider ); // when serializer.finish(); // then logProvider.assertExactly( AssertableLogProvider.inLog( ExecutionResultSerializer.class ) .error( "Unable to reply to request, because the client has closed the connection (Broken pipe)." ) ); }
@Test public void shouldLogIOErrors() { // given IOException failure = new IOException(); OutputStream output = mock( OutputStream.class, new ThrowsException( failure ) ); AssertableLogProvider logProvider = new AssertableLogProvider(); ExecutionResultSerializer serializer = getSerializerWith( output, null, logProvider ); // when serializer.finish(); // then logProvider.assertExactly( AssertableLogProvider.inLog( ExecutionResultSerializer.class ).error( is( "Failed to generate JSON output." ), sameInstance( failure ) ) ); }
@Test public void bufferCompletesWithException() throws Exception { doAnswer(new ThrowsException(new RuntimeException("Planned test exception"))) .when(processor).processMessageGroup(isA(SimpleMessageGroup.class)); String correlationKey = "key"; Message<?> message1 = testMessage(correlationKey, 1, 2); Message<?> message2 = testMessage(correlationKey, 2, 2); when(correlationStrategy.getCorrelationKey(isA(Message.class))).thenReturn(correlationKey); handler.setExpireGroupsUponCompletion(true); handler.handleMessage(message1); try { handler.handleMessage(message2); fail("Expected MessageHandlingException"); } catch (MessageHandlingException e) { assertEquals(0, store.getMessageGroup(correlationKey).size()); } verify(correlationStrategy).getCorrelationKey(message1); verify(correlationStrategy).getCorrelationKey(message2); verify(processor).processMessageGroup(isA(SimpleMessageGroup.class)); }
public Stubber doThrow(Throwable toBeThrown) { answers.add(new ThrowsException(toBeThrown)); return this; }
public DeprecatedOngoingStubbing<T> toThrow(Throwable throwable) { return toAnswer(new ThrowsException(throwable)); } }
public VoidMethodStubbable<T> toThrow(Throwable throwable) { invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(throwable)); return this; }
@Before public void setUp() throws Exception { nopeUserManager = mock(UserManager.class, new ThrowsException(new UnsupportedOperationException("NOPE"))); aUserManager = mock(UserManager.class); bUserManager = mock(UserManager.class); }
static NamespacePrefixResolver mockNamespacePrefixResolver() { NamespacePrefixResolver mockNamespacePrefixResolver = Mockito.mock(NamespacePrefixResolver.class, new ThrowsException(new NamespaceException("Mock exception "))); Mockito.doReturn(Arrays.<String> asList(SiteModel.SITE_MODEL_PREFIX)).when(mockNamespacePrefixResolver).getPrefixes(SiteModel.SITE_MODEL_URL); Mockito.doReturn(SiteModel.SITE_MODEL_URL).when(mockNamespacePrefixResolver).getNamespaceURI(SiteModel.SITE_MODEL_PREFIX); Mockito.doReturn(Arrays.<String> asList(NamespaceService.CONTENT_MODEL_PREFIX)).when(mockNamespacePrefixResolver) .getPrefixes(NamespaceService.CONTENT_MODEL_1_0_URI); Mockito.doReturn(NamespaceService.CONTENT_MODEL_1_0_URI).when(mockNamespacePrefixResolver) .getNamespaceURI(NamespaceService.CONTENT_MODEL_PREFIX); Mockito.doReturn("mock(NamespacePrefixResolver)@" + TypeVirtualizationMethod.class.toString()).when(mockNamespacePrefixResolver).toString(); return mockNamespacePrefixResolver; }
@Test public void readFileNotFound() throws Exception { mockStatic(AppCenterLog.class); FileReader fileReader = mock(FileReader.class, new ThrowsException(new FileNotFoundException())); whenNew(FileReader.class).withAnyArguments().thenReturn(fileReader); assertNull(FileManager.read(new File(""))); verify(fileReader).close(); verifyStatic(); AppCenterLog.error(anyString(), anyString(), any(IOException.class)); }
@Test public void readErrorAndCloseError() throws Exception { mockStatic(AppCenterLog.class); FileReader fileReader = mock(FileReader.class, new ThrowsException(new IOException())); whenNew(FileReader.class).withAnyArguments().thenReturn(fileReader); assertNull(FileManager.read(new File(""))); verify(fileReader).close(); verifyStatic(); AppCenterLog.error(anyString(), anyString(), any(IOException.class)); }
public void testExecuteOperationsSetsErrorOnInvalidRequestException() throws Exception { String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); OperationService service = mock(OperationService.class, new ThrowsException(new InvalidRequestException(""))); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); OperationUtil.executeOperation(operation, operationRegistry, context, ALEX); assertTrue("Expected one response", context.getResponses().size() == 1); assertTrue("Expected an error response", context.getResponse(operationId).isError()); }
public void testExecuteOperationsSetsErrorOnInvalidRequestException() throws Exception { String operationId = "op1"; OperationRequest operation = new OperationRequest("wavelet.create", operationId); OperationService service = mock(OperationService.class, new ThrowsException(new InvalidRequestException(""))); when(operationRegistry.getServiceFor(any(OperationType.class))).thenReturn(service); OperationUtil.executeOperation(operation, operationRegistry, context, ALEX); assertTrue("Expected one response", context.getResponses().size() == 1); assertTrue("Expected an error response", context.getResponse(operationId).isError()); }