@SuppressWarnings("unchecked") @Test public void shouldCallOnError() throws Exception { Callable<Object> func = mock(Callable.class); Throwable throwable = new IllegalStateException("Test exception"); when(func.call()).thenThrow(throwable); Observable<Object> fromCallableObservable = Observable.fromCallable(func); Observer<Object> observer = TestHelper.mockObserver(); fromCallableObservable.subscribe(observer); verify(observer, never()).onNext(any()); verify(observer, never()).onComplete(); verify(observer).onError(throwable); }
private void prepareWatcher( TestWatchKey watchKey ) throws InterruptedException { when( watchServiceMock.take() ).thenReturn( watchKey ) .thenThrow( InterruptedException.class ); }
@Test public void testCatchesExceptionOnReadBufferWithOffsetAndCount() throws IOException { IOException exception = new SocketTimeoutException(); when(wrapped.read(any(byte[].class), anyInt(), anyInt())).thenThrow(exception); int read = is.read(new byte[0], 10, 100); assertEquals(-1, read); assertEquals(exception, is.getException()); }
@Test public void headerConversionLazilyInvoked() throws JMSException { javax.jms.Message jmsMessage = mock(javax.jms.Message.class); when(jmsMessage.getPropertyNames()).thenThrow(new IllegalArgumentException("Header failure")); MessagingMessageListenerAdapter listener = getSimpleInstance("simple", Message.class); Message<?> message = listener.toMessagingMessage(jmsMessage); this.thrown.expect(IllegalArgumentException.class); this.thrown.expectMessage("Header failure"); message.getHeaders(); // Triggers headers resolution }
@Test void shouldHandleFailureDuringRunMessageProcessing() throws Exception { RuntimeException error = new RuntimeException( "Hello" ); when( statementProcessor.run( any(), any() ) ).thenThrow( error ); BoltStateMachineState nextState = state.process( new RunMessage( "RETURN 1", EMPTY_MAP ), context ); assertEquals( failedState, nextState ); verify( context ).handleFailure( error, false ); }
@Test public void testDeferFunctionThrows() throws Exception { Callable<Observable<String>> factory = mock(Callable.class); when(factory.call()).thenThrow(new TestException()); Observable<String> result = Observable.defer(factory); Observer<String> o = TestHelper.mockObserver(); result.subscribe(o); verify(o).onError(any(TestException.class)); verify(o, never()).onNext(any(String.class)); verify(o, never()).onComplete(); } }
@Test void shouldHandleFailuresOnInitMessage() throws Exception { RuntimeException error = new RuntimeException( "Hello" ); when( boltSpi.authenticate( AUTH_TOKEN ) ).thenThrow( error ); BoltStateMachineState newState = state.process( INIT_MESSAGE, context ); assertEquals( failedState, newState ); verify( context ).handleFailure( error, true ); }
private BoltStateMachineV3 getBoltStateMachineInFailedState() throws BoltConnectionFatality, InterruptedException { BoltStateMachineV3 machine = newStateMachine(); machine.process( newHelloMessage(), nullResponseHandler() ); RunMessage runMessage = mock( RunMessage.class ); when( runMessage.statement() ).thenThrow( new RuntimeException( "error here" ) ); BoltResponseRecorder recorder = new BoltResponseRecorder(); machine.process( runMessage, recorder ); assertThat( recorder.nextResponse(), failedWithStatus( Status.General.UnknownError ) ); assertThat( machine.state(), instanceOf( FailedState.class ) ); return machine; }
private void simulateTransportError(int delegateFrom, int count) { for (int i = 0; i < count; i++) { int delegateId = delegateFrom + i; when(clientFactory.newClient(Matchers.<EurekaEndpoint>anyVararg())).thenReturn(clusterDelegates.get(delegateId)); when(requestExecutor.execute(clusterDelegates.get(delegateId))).thenThrow(new TransportException("simulated network error")); } }
public void testAddDelayedShutdownHook_interrupted() throws InterruptedException { TestApplication application = new TestApplication(); ExecutorService service = mock(ExecutorService.class); application.addDelayedShutdownHook(service, 2, TimeUnit.SECONDS); when(service.awaitTermination(2, TimeUnit.SECONDS)).thenThrow(new InterruptedException()); application.shutdown(); verify(service).shutdown(); }
@Test void shouldHandleAuthFailureDuringRunMessageProcessing() throws Exception { AuthorizationExpiredException error = new AuthorizationExpiredException( "Hello" ); when( statementProcessor.run( any(), any() ) ).thenThrow( error ); BoltStateMachineState nextState = state.process( new RunMessage( "RETURN 1", EMPTY_MAP ), context ); assertEquals( failedState, nextState ); verify( context ).handleFailure( error, true ); }
@Test public void shouldPropagateFatalError() throws Exception { Unpacker unpacker = mock( Unpacker.class ); RuntimeException error = new RuntimeException(); when( unpacker.unpackStructHeader() ).thenThrow( error ); BoltRequestMessageReader reader = new TestBoltRequestMessageReader( connectionMock(), responseHandlerMock(), emptyList() ); RuntimeException e = assertThrows( RuntimeException.class, () -> reader.read( unpacker ) ); assertEquals( error, e ); }
@SuppressWarnings("unchecked") @Test public void shouldCallOnError() throws Exception { Callable<Object> func = mock(Callable.class); Throwable throwable = new IllegalStateException("Test exception"); when(func.call()).thenThrow(throwable); Flowable<Object> fromCallableFlowable = Flowable.fromCallable(func); Subscriber<Object> subscriber = TestHelper.mockSubscriber(); fromCallableFlowable.subscribe(subscriber); verify(subscriber, never()).onNext(any()); verify(subscriber, never()).onComplete(); verify(subscriber).onError(throwable); }
@Test void shouldReportMalformedSchemaRule() throws Exception { // given DynamicRecord badRecord = inUse( new DynamicRecord( 0 ) ); badRecord.setType( RecordAccessStub.SCHEMA_RECORD_TYPE ); when( checker().ruleAccess.loadSingleSchemaRule( 0 ) ).thenThrow( new MalformedSchemaRuleException( "Bad Record" ) ); // when ConsistencyReport.SchemaConsistencyReport report = check( badRecord ); // then verify( report ).malformedSchemaRule(); }
@Test public void testDeferFunctionThrows() throws Exception { Callable<Flowable<String>> factory = mock(Callable.class); when(factory.call()).thenThrow(new TestException()); Flowable<String> result = Flowable.defer(factory); Subscriber<String> subscriber = TestHelper.mockSubscriber(); result.subscribe(subscriber); verify(subscriber).onError(any(TestException.class)); verify(subscriber, never()).onNext(any(String.class)); verify(subscriber, never()).onComplete(); } }