From: Kai Moritz Date: Sun, 5 Jan 2025 10:34:39 +0000 (+0100) Subject: Umstellung des `ExampleConsumerTest` auf AssertJ X-Git-Url: https://juplo.de/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fconsumer%2Fspring-consumer--error-handling--offspin;p=demos%2Fkafka%2Ftraining Umstellung des `ExampleConsumerTest` auf AssertJ --- diff --git a/src/test/java/de/juplo/kafka/ExampleConsumerTest.java b/src/test/java/de/juplo/kafka/ExampleConsumerTest.java index 07164853..35be4842 100644 --- a/src/test/java/de/juplo/kafka/ExampleConsumerTest.java +++ b/src/test/java/de/juplo/kafka/ExampleConsumerTest.java @@ -36,6 +36,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import static de.juplo.kafka.ExampleConsumerTest.*; +import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest( @@ -87,7 +88,7 @@ public class ExampleConsumerTest Awaitility .await("All messages are consumed") .atMost(Duration.ofSeconds(5)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 20); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(20)); } @DisplayName("Delayed messages are consumed as expected") @@ -122,7 +123,7 @@ public class ExampleConsumerTest Awaitility .await("All messages are consumed") .atMost(Duration.ofSeconds(5)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 20); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(20)); } @DisplayName("A deserialization exception is skipped and all valid messages are consumed") @@ -155,7 +156,7 @@ public class ExampleConsumerTest Awaitility .await("All valid messages are consumed") .atMost(Duration.ofSeconds(15)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 19); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(19)); } @DisplayName("A message, that triggers an unexpected exception in the domain-logic, exits the application") @@ -189,7 +190,7 @@ public class ExampleConsumerTest .await("The ConsumerRunnable is exited by an unexpected exception") .atMost(Duration.ofSeconds(5)) .pollInterval(Duration.ofMillis(250)) - .until(() -> isTerminatedExceptionally.get()); + .untilAsserted(() -> assertThat(isTerminatedExceptionally.get()).isTrue()); } @DisplayName("A message, that triggers an non-retryable exception in the domain-logic, is skipped and all other messages are consumed") @@ -222,7 +223,7 @@ public class ExampleConsumerTest Awaitility .await("All other valid messages are consumed") .atMost(Duration.ofSeconds(15)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 19); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(19)); } @DisplayName("A message, that triggers a retryable exception in the domain-logic, is retried and all messages are eventually consumed") @@ -256,7 +257,7 @@ public class ExampleConsumerTest Awaitility .await("All messages are eventually consumed") .atMost(Duration.ofSeconds(15)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 20); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(20)); } @DisplayName("All messages on a partition are delayed and a message, that triggers a retryable exception in the domain-logic, is retried and all messages are eventually consumed") @@ -292,7 +293,7 @@ public class ExampleConsumerTest Awaitility .await("All messages are eventually consumed") .atMost(Duration.ofSeconds(15)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 20); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(20)); } @DisplayName("A message, that triggers a retryable exception in the domain-logic, but fails too often, is skipped and all other messages are eventually consumed") @@ -325,7 +326,7 @@ public class ExampleConsumerTest Awaitility .await("All other messages are eventually consumed") .atMost(Duration.ofSeconds(15)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 19); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(19)); } @DisplayName("Three messages, that trigger retryable exceptions in the domain-logic, are retried and all messages are eventually consumed") @@ -372,7 +373,7 @@ public class ExampleConsumerTest Awaitility .await("All messages are eventually consumed") .atMost(Duration.ofSeconds(20)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 30); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(30)); } @DisplayName("Three messages, that trigger retryable exceptions in the domain-logic, are retried, but one of them fails too often and is skipped, still all other messages are eventually consumed") @@ -419,7 +420,7 @@ public class ExampleConsumerTest Awaitility .await("All other messages are eventually consumed") .atMost(Duration.ofSeconds(20)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 29); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(29)); } @DisplayName("A message, that triggers a retryable exception in the domain-logic, is retried 3 times with a fixed back-of and all messages are eventually consumed") @@ -454,7 +455,7 @@ public class ExampleConsumerTest Awaitility .await("All messages are eventually consumed") .atMost(Duration.ofSeconds(15)) - .until(() -> mockRecordHandler.getNumMessagesHandled() == 20); + .untilAsserted(() -> assertThat(mockRecordHandler.getNumMessagesHandled()).isEqualTo(20)); }