From 330b7238f03cdfafd72f9980ce0f019c46b02afe Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Sun, 12 Jun 2022 11:57:33 +0200 Subject: [PATCH] =?utf8?q?Testfall,=20der=20die=20API=20aufruft=20und=20pr?= =?utf8?q?=C3=BCft,=20ob=20Nachrichten=20versendet=20werden?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * Der Testfall verwendet `spring-kafka` als Abhängigkeit mit dem Scope `test`. * Dies zeigt ein nicht zu unterschätzendes Anwendungsfeld für Spring Kafka, seblst wenn das eigentliche Projekt gar nicht auf Spring Kafka aufsetzt: Einfache Implementierung von Integration-Tests. --- pom.xml | 15 ++++ .../java/de/juplo/kafka/ApplicationTests.java | 86 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 src/test/java/de/juplo/kafka/ApplicationTests.java diff --git a/pom.xml b/pom.xml index d02cfc0..46a8165 100644 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,21 @@ spring-boot-starter-test test + + org.springframework.kafka + spring-kafka + test + + + org.springframework.kafka + spring-kafka-test + test + + + org.awaitility + awaitility + test + diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java new file mode 100644 index 0000000..cf70c81 --- /dev/null +++ b/src/test/java/de/juplo/kafka/ApplicationTests.java @@ -0,0 +1,86 @@ +package de.juplo.kafka; + +import lombok.extern.slf4j.Slf4j; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.kafka.test.context.EmbeddedKafka; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.Duration; +import java.util.LinkedList; +import java.util.List; + +import static de.juplo.kafka.ApplicationTests.PARTITIONS; +import static de.juplo.kafka.ApplicationTests.TOPIC; +import static org.awaitility.Awaitility.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@SpringBootTest( + properties = { + "spring.kafka.consumer.bootstrap-servers=${spring.embedded.kafka.brokers}", + "producer.bootstrap-server=${spring.embedded.kafka.brokers}", + "producer.topic=" + TOPIC}) +@AutoConfigureMockMvc +@EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS) +@Slf4j +public class ApplicationTests +{ + static final String TOPIC = "FOO"; + static final int PARTITIONS = 10; + + @Autowired + MockMvc mockMvc; + @Autowired + Consumer consumer; + + + @BeforeEach + public void clear() + { + consumer.received.clear(); + } + + + @Test + void testSendMessage() throws Exception + { + mockMvc + .perform(post("/peter").content("Hallo Welt!")) + .andExpect(status().isOk()); + await("Message was send") + .atMost(Duration.ofSeconds(5)) + .until(() -> consumer.received.size() == 1); + } + + + static class Consumer + { + final List> received = new LinkedList<>(); + + @KafkaListener(groupId = "TEST", topics = TOPIC) + public void receive(ConsumerRecord record) + { + log.debug("Received message: {}", record); + received.add(record); + } + } + + @TestConfiguration + static class Configuration + { + @Bean + Consumer consumer() + { + return new Consumer(); + } + } +} -- 2.20.1