From ae04ac51dae8d4d8f7d3434e7ddf7a8b40bd5eac Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sat, 8 Jun 2024 09:36:43 +0200 Subject: [PATCH] counter: 1.2.15 - Refined `CounterApplicationIT` * Preparations for the addition of new tests * The messages are send only once during `@BeforeAll` --- .../counter/CounterApplicationIT.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java b/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java index cb0a5b7..41e5b62 100644 --- a/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java +++ b/src/test/java/de/juplo/kafka/wordcount/counter/CounterApplicationIT.java @@ -6,7 +6,8 @@ import de.juplo.kafka.wordcount.top10.TestOutputWordCounter; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.streams.state.KeyValueBytesStoreSupplier; import org.apache.kafka.streams.state.Stores; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -16,6 +17,7 @@ import org.springframework.context.annotation.Primary; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.kafka.support.KafkaHeaders; +import org.springframework.kafka.support.SendResult; import org.springframework.kafka.test.context.EmbeddedKafka; import org.springframework.messaging.handler.annotation.Header; import org.springframework.messaging.handler.annotation.Payload; @@ -51,26 +53,39 @@ public class CounterApplicationIT public static final String TOPIC_IN = "in"; public static final String TOPIC_OUT = "out"; - @Autowired - KafkaTemplate kafkaTemplate; @Autowired Consumer consumer; - @BeforeEach - public void clear() + @BeforeAll + public static void testSendMessage( + @Autowired KafkaTemplate kafkaTemplate) { - consumer.received.clear(); + TestData + .getInputMessages() + .forEach(kv -> + { + try + { + SendResult result = kafkaTemplate.send(TOPIC_IN, kv.key, kv.value).get(); + log.info( + "Sent: {}={}, partition={}, offset={}", + result.getProducerRecord().key(), + result.getProducerRecord().value(), + result.getRecordMetadata().partition(), + result.getRecordMetadata().offset()); + } + catch (Exception e) + { + throw new RuntimeException(e); + } + }); } - + @DisplayName("Await the expected output messages") @Test void testSendMessage() { - TestData - .getInputMessages() - .forEach(kv -> kafkaTemplate.send(TOPIC_IN, kv.key, kv.value)); - await("Expected messages") .atMost(Duration.ofSeconds(10)) .untilAsserted(() -> consumer.enforceAssertion(TestData.expectedMessagesAssertion())); -- 2.20.1