Testfall, der die API aufruft und prüft, ob Nachrichten versendet werden
authorKai Moritz <kai@juplo.de>
Sun, 12 Jun 2022 09:57:33 +0000 (11:57 +0200)
committerKai Moritz <kai@juplo.de>
Sun, 12 Jun 2022 13:21:13 +0000 (15:21 +0200)
* 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
src/test/java/de/juplo/kafka/ApplicationTests.java [new file with mode: 0644]

diff --git a/pom.xml b/pom.xml
index d02cfc0..46a8165 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       <artifactId>spring-boot-starter-test</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.springframework.kafka</groupId>
+      <artifactId>spring-kafka</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework.kafka</groupId>
+      <artifactId>spring-kafka-test</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.awaitility</groupId>
+      <artifactId>awaitility</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
diff --git a/src/test/java/de/juplo/kafka/ApplicationTests.java b/src/test/java/de/juplo/kafka/ApplicationTests.java
new file mode 100644 (file)
index 0000000..cf70c81
--- /dev/null
@@ -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<ConsumerRecord<String, String>> received = new LinkedList<>();
+
+               @KafkaListener(groupId = "TEST", topics = TOPIC)
+               public void receive(ConsumerRecord<String, String> record)
+               {
+                       log.debug("Received message: {}", record);
+                       received.add(record);
+               }
+       }
+
+       @TestConfiguration
+       static class Configuration
+       {
+               @Bean
+               Consumer consumer()
+               {
+                       return new Consumer();
+               }
+       }
+}