From b18053f93a2f3b01c53843284627a54e751577cf Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 13 Apr 2015 15:31:35 -0400 Subject: [PATCH] Fix failing test The onFailure callback and future.get() occur in different threads so this change adds a latch to ensure we have both before asserting. Issue: SPR-12887 --- .../web/client/AsyncRestTemplateIntegrationTests.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 22ad9cd5ee..9a45ff0276 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 @@ -20,8 +20,10 @@ import java.net.URI; import java.nio.charset.Charset; import java.util.EnumSet; import java.util.Set; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; import org.junit.Before; import org.junit.Test; @@ -321,6 +323,7 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa public void identicalExceptionThroughGetAndCallback() throws Exception { final HttpClientErrorException[] callbackException = new HttpClientErrorException[1]; + final CountDownLatch latch = new CountDownLatch(1); ListenableFuture future = template.execute(baseUrl + "/status/notfound", HttpMethod.GET, null, null); future.addCallback(new ListenableFutureCallback() { @Override @@ -328,9 +331,10 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa fail("onSuccess not expected"); } @Override - public void onFailure(Throwable t) { - assertTrue(t instanceof HttpClientErrorException); - callbackException[0] = (HttpClientErrorException) t; + public void onFailure(Throwable ex) { + assertTrue(ex instanceof HttpClientErrorException); + callbackException[0] = (HttpClientErrorException) ex; + latch.countDown(); } }); @@ -341,6 +345,7 @@ public class AsyncRestTemplateIntegrationTests extends AbstractJettyServerTestCa catch (ExecutionException ex) { Throwable cause = ex.getCause(); assertTrue(cause instanceof HttpClientErrorException); + latch.await(5, TimeUnit.SECONDS); assertSame(callbackException[0], cause); } }