Skip to content

Commit 9fed0a1

Browse files
committed
AssertJ CompletableFuture assertions
1 parent 76a571c commit 9fed0a1

File tree

1 file changed

+68
-71
lines changed

1 file changed

+68
-71
lines changed

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/AsyncRabbitTemplateTests.java

Lines changed: 68 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
21-
import static org.assertj.core.api.Assertions.fail;
2221
import static org.awaitility.Awaitility.await;
2322
import static org.mockito.Mockito.mock;
2423

@@ -102,7 +101,7 @@ static void setup() {
102101
}
103102

104103
@Test
105-
public void testConvert1Arg() throws Exception {
104+
public void testConvert1Arg() {
106105
final AtomicBoolean mppCalled = new AtomicBoolean();
107106
CompletableFuture<String> future = this.asyncTemplate.convertSendAndReceive("foo", m -> {
108107
mppCalled.set(true);
@@ -113,7 +112,7 @@ public void testConvert1Arg() throws Exception {
113112
}
114113

115114
@Test
116-
public void testConvert1ArgDirect() throws Exception {
115+
public void testConvert1ArgDirect() {
117116
this.latch.set(new CountDownLatch(1));
118117
CompletableFuture<String> future1 = this.asyncDirectTemplate.convertSendAndReceive("foo");
119118
CompletableFuture<String> future2 = this.asyncDirectTemplate.convertSendAndReceive("bar");
@@ -141,19 +140,19 @@ public void testConvert1ArgDirect() throws Exception {
141140
}
142141

143142
@Test
144-
public void testConvert2Args() throws Exception {
143+
public void testConvert2Args() {
145144
CompletableFuture<String> future = this.asyncTemplate.convertSendAndReceive(this.requests.getName(), "foo");
146145
checkConverterResult(future, "FOO");
147146
}
148147

149148
@Test
150-
public void testConvert3Args() throws Exception {
149+
public void testConvert3Args() {
151150
CompletableFuture<String> future = this.asyncTemplate.convertSendAndReceive("", this.requests.getName(), "foo");
152151
checkConverterResult(future, "FOO");
153152
}
154153

155154
@Test
156-
public void testConvert4Args() throws Exception {
155+
public void testConvert4Args() {
157156
CompletableFuture<String> future = this.asyncTemplate.convertSendAndReceive("", this.requests.getName(), "foo",
158157
message -> {
159158
String body = new String(message.getBody());
@@ -189,7 +188,7 @@ public void testMessage1ArgDirect() throws Exception {
189188
assertThat(TestUtils
190189
.getPropertyValue(this.asyncDirectTemplate, "directReplyToContainer.consumerCount",
191190
AtomicInteger.class).get())
192-
.isEqualTo(0);
191+
.isZero();
193192
}
194193

195194
private void waitForZeroInUseConsumers() {
@@ -216,7 +215,7 @@ public void testMessage3Args() throws Exception {
216215
public void testCancel() {
217216
CompletableFuture<String> future = this.asyncTemplate.convertSendAndReceive("foo");
218217
future.cancel(false);
219-
assertThat(TestUtils.getPropertyValue(asyncTemplate, "pending", Map.class)).hasSize(0);
218+
assertThat(TestUtils.getPropertyValue(asyncTemplate, "pending", Map.class)).isEmpty();
220219
}
221220

222221
@Test
@@ -236,44 +235,46 @@ private Message getFooMessage() {
236235

237236
@Test
238237
@DirtiesContext
239-
public void testReturn() throws Exception {
238+
public void testReturn() {
240239
this.asyncTemplate.setMandatory(true);
241240
CompletableFuture<String> future = this.asyncTemplate.convertSendAndReceive(this.requests.getName() + "x",
242241
"foo");
243-
try {
244-
future.get(10, TimeUnit.SECONDS);
245-
fail("Expected exception");
246-
}
247-
catch (ExecutionException e) {
248-
assertThat(e.getCause()).isInstanceOf(AmqpMessageReturnedException.class);
249-
assertThat(((AmqpMessageReturnedException) e.getCause()).getRoutingKey()).isEqualTo(this.requests.getName() + "x");
250-
}
242+
assertThat(future)
243+
.as("Expected exception")
244+
.failsWithin(Duration.ofSeconds(10))
245+
.withThrowableOfType(ExecutionException.class)
246+
.havingCause()
247+
.isInstanceOf(AmqpMessageReturnedException.class)
248+
.extracting(cause -> ((AmqpMessageReturnedException) cause).getRoutingKey())
249+
.isEqualTo(this.requests.getName() + "x");
251250
}
252251

253252
@Test
254253
@DirtiesContext
255-
public void testReturnDirect() throws Exception {
254+
public void testReturnDirect() {
256255
this.asyncDirectTemplate.setMandatory(true);
257256
CompletableFuture<String> future = this.asyncDirectTemplate.convertSendAndReceive(this.requests.getName() + "x",
258257
"foo");
259-
try {
260-
future.get(10, TimeUnit.SECONDS);
261-
fail("Expected exception");
262-
}
263-
catch (ExecutionException e) {
264-
assertThat(e.getCause()).isInstanceOf(AmqpMessageReturnedException.class);
265-
assertThat(((AmqpMessageReturnedException) e.getCause()).getRoutingKey()).isEqualTo(this.requests.getName() + "x");
266-
}
258+
259+
assertThat(future)
260+
.as("Expected exception")
261+
.failsWithin(Duration.ofSeconds(10))
262+
.withThrowableOfType(ExecutionException.class)
263+
.havingCause()
264+
.isInstanceOf(AmqpMessageReturnedException.class)
265+
.extracting(cause -> ((AmqpMessageReturnedException) cause).getRoutingKey())
266+
.isEqualTo(this.requests.getName() + "x");
267267
}
268268

269269
@Test
270270
@DirtiesContext
271-
public void testConvertWithConfirm() throws Exception {
271+
public void testConvertWithConfirm() {
272272
this.asyncTemplate.setEnableConfirms(true);
273273
RabbitConverterFuture<String> future = this.asyncTemplate.convertSendAndReceive("sleep");
274274
CompletableFuture<Boolean> confirm = future.getConfirm();
275-
assertThat(confirm).isNotNull();
276-
assertThat(confirm.get(10, TimeUnit.SECONDS)).isTrue();
275+
assertThat(confirm).isNotNull()
276+
.succeedsWithin(Duration.ofSeconds(10))
277+
.isEqualTo(true);
277278
checkConverterResult(future, "SLEEP");
278279
}
279280

@@ -284,19 +285,21 @@ public void testMessageWithConfirm() throws Exception {
284285
RabbitMessageFuture future = this.asyncTemplate
285286
.sendAndReceive(new SimpleMessageConverter().toMessage("sleep", new MessageProperties()));
286287
CompletableFuture<Boolean> confirm = future.getConfirm();
287-
assertThat(confirm).isNotNull();
288-
assertThat(confirm.get(10, TimeUnit.SECONDS)).isTrue();
288+
assertThat(confirm).isNotNull()
289+
.succeedsWithin(Duration.ofSeconds(10))
290+
.isEqualTo(true);
289291
checkMessageResult(future, "SLEEP");
290292
}
291293

292294
@Test
293295
@DirtiesContext
294-
public void testConvertWithConfirmDirect() throws Exception {
296+
public void testConvertWithConfirmDirect() {
295297
this.asyncDirectTemplate.setEnableConfirms(true);
296298
RabbitConverterFuture<String> future = this.asyncDirectTemplate.convertSendAndReceive("sleep");
297299
CompletableFuture<Boolean> confirm = future.getConfirm();
298-
assertThat(confirm).isNotNull();
299-
assertThat(confirm.get(10, TimeUnit.SECONDS)).isTrue();
300+
assertThat(confirm).isNotNull()
301+
.succeedsWithin(Duration.ofSeconds(10))
302+
.isEqualTo(true);
300303
checkConverterResult(future, "SLEEP");
301304
}
302305

@@ -307,8 +310,9 @@ public void testMessageWithConfirmDirect() throws Exception {
307310
RabbitMessageFuture future = this.asyncDirectTemplate
308311
.sendAndReceive(new SimpleMessageConverter().toMessage("sleep", new MessageProperties()));
309312
CompletableFuture<Boolean> confirm = future.getConfirm();
310-
assertThat(confirm).isNotNull();
311-
assertThat(confirm.get(10, TimeUnit.SECONDS)).isTrue();
313+
assertThat(confirm).isNotNull()
314+
.succeedsWithin(Duration.ofSeconds(10))
315+
.isEqualTo(true);
312316
checkMessageResult(future, "SLEEP");
313317
}
314318

@@ -321,14 +325,12 @@ public void testReceiveTimeout() throws Exception {
321325
TheCallback callback = new TheCallback();
322326
future.whenComplete(callback);
323327
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class)).hasSize(1);
324-
try {
325-
future.get(10, TimeUnit.SECONDS);
326-
fail("Expected ExecutionException");
327-
}
328-
catch (ExecutionException e) {
329-
assertThat(e.getCause()).isInstanceOf(AmqpReplyTimeoutException.class);
330-
}
331-
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class)).hasSize(0);
328+
assertThat(future)
329+
.as("Expected ExecutionException")
330+
.failsWithin(Duration.ofSeconds(10))
331+
.withThrowableOfType(ExecutionException.class)
332+
.withCauseInstanceOf(AmqpReplyTimeoutException.class);
333+
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class)).isEmpty();
332334
assertThat(callback.latch.await(10, TimeUnit.SECONDS)).isTrue();
333335
assertThat(callback.ex).isInstanceOf(AmqpReplyTimeoutException.class);
334336
}
@@ -342,14 +344,13 @@ public void testReplyAfterReceiveTimeout() throws Exception {
342344
TheCallback callback = new TheCallback();
343345
future.whenComplete(callback);
344346
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class)).hasSize(1);
345-
try {
346-
future.get(10, TimeUnit.SECONDS);
347-
fail("Expected ExecutionException");
348-
}
349-
catch (ExecutionException e) {
350-
assertThat(e.getCause()).isInstanceOf(AmqpReplyTimeoutException.class);
351-
}
352-
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class)).hasSize(0);
347+
348+
assertThat(future)
349+
.as("Expected ExecutionException")
350+
.failsWithin(Duration.ofSeconds(10))
351+
.withThrowableOfType(ExecutionException.class)
352+
.withCauseInstanceOf(AmqpReplyTimeoutException.class);
353+
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class)).isEmpty();
353354
assertThat(callback.latch.await(10, TimeUnit.SECONDS)).isTrue();
354355
assertThat(callback.ex).isInstanceOf(AmqpReplyTimeoutException.class);
355356

@@ -375,16 +376,17 @@ public void testStopCancelled() throws Exception {
375376
this.asyncTemplate.stop();
376377
// Second stop() to be sure that it is idempotent
377378
this.asyncTemplate.stop();
378-
try {
379-
future.get(10, TimeUnit.SECONDS);
380-
fail("Expected CancellationException");
381-
}
382-
catch (CancellationException e) {
383-
assertThat(future.getNackCause()).isEqualTo("AsyncRabbitTemplate was stopped while waiting for reply");
384-
}
385-
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class)).hasSize(0);
379+
assertThat(future)
380+
.as("Expected CancellationException")
381+
.failsWithin(Duration.ofSeconds(10))
382+
.withThrowableOfType(CancellationException.class)
383+
.satisfies(e -> {
384+
assertThat(future.getNackCause()).isEqualTo("AsyncRabbitTemplate was stopped while waiting for reply");
385+
assertThat(future).isCancelled();
386+
});
387+
388+
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "pending", Map.class)).isEmpty();
386389
assertThat(callback.latch.await(10, TimeUnit.SECONDS)).isTrue();
387-
assertThat(future.isCancelled()).isTrue();
388390
assertThat(TestUtils.getPropertyValue(this.asyncTemplate, "taskScheduler")).isNull();
389391

390392
/*
@@ -398,7 +400,7 @@ public void testStopCancelled() throws Exception {
398400

399401
@Test
400402
@DirtiesContext
401-
public void testConversionException() throws InterruptedException {
403+
public void testConversionException() {
402404
this.asyncTemplate.getRabbitTemplate().setMessageConverter(new SimpleMessageConverter() {
403405
@Override
404406
public Object fromMessage(Message message) throws MessageConversionException {
@@ -480,15 +482,10 @@ public void limitedChannelsAreReleasedOnTimeout() {
480482
connectionFactory.destroy();
481483
}
482484

483-
private void checkConverterResult(CompletableFuture<String> future, String expected) throws InterruptedException {
484-
final CountDownLatch cdl = new CountDownLatch(1);
485-
final AtomicReference<String> resultRef = new AtomicReference<>();
486-
future.whenComplete((result, ex) -> {
487-
resultRef.set(result);
488-
cdl.countDown();
489-
});
490-
assertThat(cdl.await(10, TimeUnit.SECONDS)).isTrue();
491-
assertThat(resultRef.get()).isEqualTo(expected);
485+
private void checkConverterResult(CompletableFuture<String> future, String expected) {
486+
assertThat(future)
487+
.succeedsWithin(Duration.ofSeconds(10))
488+
.isEqualTo(expected);
492489
}
493490

494491
private Message checkMessageResult(CompletableFuture<Message> future, String expected) throws InterruptedException {

0 commit comments

Comments
 (0)