diff --git a/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java b/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java index 86b436ba588..6fef1b39e08 100644 --- a/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java +++ b/spring-web/src/test/java/org/springframework/web/client/AsyncRestTemplateIntegrationTests.java @@ -599,25 +599,20 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa template.setInterceptors(Collections.singletonList(interceptor)); ListenableFuture> future = template.getForEntity("/get", String.class); - ResponseEntity response = future.get(); + interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.OK, interceptor.response.getStatusCode()); assertNull(interceptor.exception); - assertEquals(helloWorld, response.getBody()); + assertEquals(helloWorld, future.get().getBody()); } @Test public void getAndInterceptError() throws Exception { RequestInterceptor interceptor = new RequestInterceptor(); template.setInterceptors(Collections.singletonList(interceptor)); - ListenableFuture> future = template.getForEntity("/status/notfound", String.class); + template.getForEntity("/status/notfound", String.class); - try { - future.get(); - fail("No exception thrown"); - } catch (ExecutionException ex) { - // expected - } + interceptor.latch.await(5, TimeUnit.SECONDS); assertNotNull(interceptor.response); assertEquals(HttpStatus.NOT_FOUND, interceptor.response.getStatusCode()); assertNull(interceptor.exception); @@ -632,6 +627,8 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa private static class RequestInterceptor implements AsyncClientHttpRequestInterceptor { + private final CountDownLatch latch = new CountDownLatch(1); + private volatile ClientHttpResponse response; private volatile Throwable exception; @@ -654,7 +651,15 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa }; ListenableFuture future = execution.executeAsync(request, body); - future.addCallback(resp -> response = resp, ex -> exception = ex); + future.addCallback( + resp -> { + response = resp; + this.latch.countDown(); + }, + ex -> { + exception = ex; + this.latch.countDown(); + }); return future; } }