@Test public void handleReturnValueWithMultipleHandlers() throws Exception { HandlerMethodReturnValueHandler anotherIntegerHandler = mock(HandlerMethodReturnValueHandler.class); when(anotherIntegerHandler.supportsReturnType(this.integerType)).thenReturn(true); this.handlers.handleReturnValue(55, this.integerType, this.mavContainer, null); verify(this.integerHandler).handleReturnValue(55, this.integerType, this.mavContainer, null); verifyNoMoreInteractions(anotherIntegerHandler); }
@Test public void afterConnectFailure() { IllegalStateException exception = new IllegalStateException("simulated exception"); this.session.afterConnectFailure(exception); verify(this.sessionHandler).handleTransportError(this.session, exception); verifyNoMoreInteractions(this.sessionHandler); }
private static void verifyObserver(Observer<Integer> mock, int numSubscriptions, int numItemsExpected, Throwable error) { verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); verify(mock, times(numSubscriptions)).onError(error); verifyNoMoreInteractions(mock); }
@Test public void handleMessageEmptyPayload() throws Exception { this.session.handleMessage(new TextMessage(""), this.webSocketSession); verifyNoMoreInteractions(this.webSocketHandler); }
private WebSocketHandler connect() { this.stompClient.connect("/foo", mock(StompSessionHandler.class)); verify(this.stompSession).getSessionFuture(); verifyNoMoreInteractions(this.stompSession); WebSocketHandler webSocketHandler = this.webSocketHandlerCaptor.getValue(); assertNotNull(webSocketHandler); return webSocketHandler; }
@Test @SuppressWarnings({"rawtypes", "unchecked"}) public void cancelInactivityTasks() throws Exception { TcpConnection<byte[]> tcpConnection = getTcpConnection(); ScheduledFuture future = mock(ScheduledFuture.class); when(this.taskScheduler.scheduleWithFixedDelay(any(), eq(1L))).thenReturn(future); tcpConnection.onReadInactivity(mock(Runnable.class), 2L); tcpConnection.onWriteInactivity(mock(Runnable.class), 2L); this.webSocketHandlerCaptor.getValue().afterConnectionClosed(this.webSocketSession, CloseStatus.NORMAL); verify(future, times(2)).cancel(true); verifyNoMoreInteractions(future); }
@Test public void handleFailure() { IllegalStateException exception = new IllegalStateException("simulated exception"); this.session.handleFailure(exception); verify(this.sessionHandler).handleTransportError(this.session, exception); verifyNoMoreInteractions(this.sessionHandler); }
@Test public void handleWebSocketMessagePong() throws Exception { connect().handleMessage(this.webSocketSession, new PongMessage()); verifyNoMoreInteractions(this.stompSession); }
@GwtIncompatible // Mockito public void testOnSuccessThrowsRuntimeException() throws Exception { RuntimeException exception = new RuntimeException(); String result = "result"; SettableFuture<String> future = SettableFuture.create(); @SuppressWarnings("unchecked") // Safe for a mock FutureCallback<String> callback = Mockito.mock(FutureCallback.class); addCallback(future, callback, directExecutor()); Mockito.doThrow(exception).when(callback).onSuccess(result); future.set(result); assertEquals(result, future.get()); Mockito.verify(callback).onSuccess(result); Mockito.verifyNoMoreInteractions(callback); }
private static void verifyObserver(Subscriber<Integer> mock, int numSubscriptions, int numItemsExpected, Throwable error) { verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); verify(mock, times(numSubscriptions)).onError(error); verifyNoMoreInteractions(mock); }
@SuppressWarnings("unchecked") @Test public void startAndStopWithHeartbeatValue() { ScheduledFuture future = mock(ScheduledFuture.class); when(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), eq(15000L))).thenReturn(future); this.messageHandler.setTaskScheduler(this.taskScheduler); this.messageHandler.setHeartbeatValue(new long[] {15000, 16000}); this.messageHandler.start(); verify(this.taskScheduler).scheduleWithFixedDelay(any(Runnable.class), eq(15000L)); verifyNoMoreInteractions(this.taskScheduler, future); this.messageHandler.stop(); verify(future).cancel(true); verifyNoMoreInteractions(future); }
@Test public void afterConnectionClosed() { this.session.afterConnectionClosed(); verify(this.sessionHandler).handleTransportError(same(this.session), any(ConnectionLostException.class)); verifyNoMoreInteractions(this.sessionHandler); }
@Test public void uriBuilderWithPathOverride() { this.builder.build().get() .uri(builder -> builder.replacePath("/path").build()) .exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("/path", request.url().toString()); verifyNoMoreInteractions(this.exchangeFunction); }
public void testStandardValues() throws InvocationTargetException { @SuppressWarnings("unchecked") final Map<String, Boolean> map = mock(Map.class); Map<String, Boolean> forward = new ForwardingMap<String, Boolean>() { @Override protected Map<String, Boolean> delegate() { return map; } @Override public Collection<Boolean> values() { return new StandardValues(); } }; callAllPublicMethods(new TypeToken<Collection<Boolean>>() {}, forward.values()); // These are the methods specified by StandardValues verify(map, atLeast(0)).clear(); verify(map, atLeast(0)).containsValue(anyObject()); verify(map, atLeast(0)).isEmpty(); verify(map, atLeast(0)).size(); verify(map, atLeast(0)).entrySet(); verifyNoMoreInteractions(map); }
private static void verifyObserverMock(Subscriber<Integer> mock, int numSubscriptions, int numItemsExpected) { verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); verify(mock, times(numSubscriptions)).onComplete(); verifyNoMoreInteractions(mock); }
@SuppressWarnings("unchecked") @Test public void brokerUnavailableEvent() throws Exception { ScheduledFuture future = Mockito.mock(ScheduledFuture.class); when(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), any(Long.class))).thenReturn(future); BrokerAvailabilityEvent event = new BrokerAvailabilityEvent(true, this); this.handler.onApplicationEvent(event); verifyNoMoreInteractions(future); event = new BrokerAvailabilityEvent(false, this); this.handler.onApplicationEvent(event); verify(future).cancel(true); }
@Test public void shouldAllowToThrowCheckedException() { final Exception checkedException = new Exception("test exception"); Flowable<Object> fromCallableFlowable = Flowable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw checkedException; } }); Subscriber<Object> subscriber = TestHelper.mockSubscriber(); fromCallableFlowable.subscribe(subscriber); verify(subscriber).onSubscribe(any(Subscription.class)); verify(subscriber).onError(checkedException); verifyNoMoreInteractions(subscriber); }
@Test public void uriBuilder() { this.builder.build().get() .uri(builder -> builder.path("/path").queryParam("q", "12").build()) .exchange(); ClientRequest request = verifyAndGetRequest(); assertEquals("/base/path?q=12", request.url().toString()); verifyNoMoreInteractions(this.exchangeFunction); }
@GwtIncompatible // Mockito public void testOnSuccessThrowsError() throws Exception { class TestError extends Error {} TestError error = new TestError(); String result = "result"; SettableFuture<String> future = SettableFuture.create(); @SuppressWarnings("unchecked") // Safe for a mock FutureCallback<String> callback = Mockito.mock(FutureCallback.class); addCallback(future, callback, directExecutor()); Mockito.doThrow(error).when(callback).onSuccess(result); try { future.set(result); fail("Should have thrown"); } catch (TestError e) { assertSame(error, e); } assertEquals(result, future.get()); Mockito.verify(callback).onSuccess(result); Mockito.verifyNoMoreInteractions(callback); }
private static void verifyObserverMock(Observer<Integer> mock, int numSubscriptions, int numItemsExpected) { verify(mock, times(numItemsExpected)).onNext((Integer) notNull()); verify(mock, times(numSubscriptions)).onComplete(); verifyNoMoreInteractions(mock); }