Source:
<code>public void testShutdownWhileWaitingForReply() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); given(mockConnection.isOpen()).willReturn(true); given(mockConnection.createChannel()).willReturn(mockChannel); given(mockChannel.queueDeclare()).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference<ShutdownListener> listener = new AtomicReference<>(); final CountDownLatch shutdownLatch = new CountDownLatch(1); willAnswer(invocation -> { listener.set(invocation.getArgument(0)); shutdownLatch.countDown(); return null; }).given(mockChannel).addShutdownListener(any()); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setReplyTimeout(60_000); Message input = new Message("Hello, world!".getBytes(), new MessageProperties()); <START> Executors.newSingleThreadExecutor().execute(() -> { <END> try { shutdownLatch.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } listener.get().shutdownCompleted(new ShutdownSignalException(true, false, null, null)); }); try { template.doSendAndReceiveWithTemporary("foo", "bar", input, null); fail("Expected exception"); } catch (AmqpException e) { assertThat(e.getCause(), instanceOf(ShutdownSignalException.class)); } }</code><technical_language>need kill thread pool in end of test? point is of this: public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); } I dies, of course, in end of JVM, hang test suit execution</technical_language>

Target:
public void testShutdownWhileWaitingForReply() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); given(mockConnection.isOpen()).willReturn(true); given(mockConnection.createChannel()).willReturn(mockChannel); given(mockChannel.queueDeclare()).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference<ShutdownListener> listener = new AtomicReference<>(); final CountDownLatch shutdownLatch = new CountDownLatch(1); willAnswer(invocation -> { listener.set(invocation.getArgument(0)); shutdownLatch.countDown(); return null; }).given(mockChannel).addShutdownListener(any()); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setReplyTimeout(60_000); Message input = new Message("Hello, world!".getBytes(), new MessageProperties()); ExecutorService exec = Executors.newSingleThreadExecutor(); exec.execute(() -> { try { shutdownLatch.await(10, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } listener.get().shutdownCompleted(new ShutdownSignalException(true, false, null, null)); }); try { template.doSendAndReceiveWithTemporary("foo", "bar", input, null); fail("Expected exception"); } catch (AmqpException e) { assertThat(e.getCause(), instanceOf(ShutdownSignalException.class)); } exec.shutdownNow(); }