Examples für `@SpringBootTest` und `@SpringJunitConfig`
authorKai Moritz <kai@juplo.de>
Fri, 5 Jul 2024 17:23:20 +0000 (19:23 +0200)
committerKai Moritz <kai@juplo.de>
Fri, 5 Jul 2024 18:55:51 +0000 (20:55 +0200)
src/test/java/de/juplo/kafka/wordcount/counter/SpringBootTestExampleTest.java [new file with mode: 0644]
src/test/java/de/juplo/kafka/wordcount/counter/SpringJunitConfigExampleTest.java [new file with mode: 0644]

diff --git a/src/test/java/de/juplo/kafka/wordcount/counter/SpringBootTestExampleTest.java b/src/test/java/de/juplo/kafka/wordcount/counter/SpringBootTestExampleTest.java
new file mode 100644 (file)
index 0000000..8088427
--- /dev/null
@@ -0,0 +1,53 @@
+package de.juplo.kafka.wordcount.counter;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+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.core.KafkaTemplate;
+import org.springframework.kafka.support.KafkaHeaders;
+import org.springframework.kafka.test.context.EmbeddedKafka;
+import org.springframework.messaging.handler.annotation.Header;
+import org.springframework.messaging.handler.annotation.Payload;
+import org.springframework.test.annotation.DirtiesContext;
+
+
+@SpringBootTest
+@EmbeddedKafka(topics = "TEST")
+@DirtiesContext
+@Slf4j
+public class SpringBootTestExampleTest
+{
+  @Test
+  void testC(
+      @Autowired KafkaTemplate<String, String> template,
+      @Autowired Consumer consumer)
+  {
+    template.send("TEST", "peter", "Hallo Welt!");
+  }
+
+  static class Consumer
+  {
+    @KafkaListener(groupId = "test", topics = "TEST")
+    public void receive(
+        @Header(KafkaHeaders.RECEIVED_KEY) String key,
+        @Payload String value)
+    {
+      log.info("Received {}={}", key, value);
+    }
+  }
+
+  @TestConfiguration
+  static class Config
+  {
+    @Bean
+    Consumer consumer()
+    {
+      return new Consumer();
+    }
+  }
+
+}
diff --git a/src/test/java/de/juplo/kafka/wordcount/counter/SpringJunitConfigExampleTest.java b/src/test/java/de/juplo/kafka/wordcount/counter/SpringJunitConfigExampleTest.java
new file mode 100644 (file)
index 0000000..ae2b22e
--- /dev/null
@@ -0,0 +1,31 @@
+package de.juplo.kafka.wordcount.counter;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.kafka.core.KafkaTemplate;
+import org.springframework.kafka.test.EmbeddedKafkaBroker;
+import org.springframework.kafka.test.context.EmbeddedKafka;
+import org.springframework.test.annotation.DirtiesContext;
+import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
+
+
+@SpringJUnitConfig
+@EmbeddedKafka(topics = "TEST")
+@DirtiesContext
+@Slf4j
+public class SpringJunitConfigExampleTest
+{
+  @Test
+  void testValue(@Value("${spring.embedded.kafka.brokers}") String brokers)
+  {
+    log.info("Broker-Adress: {}", brokers);
+  }
+
+  @Test
+  void testEmbeddedKafkaBroker(@Autowired EmbeddedKafkaBroker broker)
+  {
+    log.info("Broker-Adress: {}", broker.getBrokersAsString());
+  }
+}