From 604d898623ada1828008e156988cab3d48e7b55a Mon Sep 17 00:00:00 2001 From: Kai Moritz Date: Fri, 5 Jul 2024 19:23:20 +0200 Subject: [PATCH] =?utf8?q?Examples=20f=C3=BCr=20`@SpringBootTest`=20und=20?= =?utf8?q?`@SpringJunitConfig`?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../counter/SpringBootTestExampleTest.java | 53 +++++++++++++++++++ .../counter/SpringJunitConfigExampleTest.java | 31 +++++++++++ 2 files changed, 84 insertions(+) create mode 100644 src/test/java/de/juplo/kafka/wordcount/counter/SpringBootTestExampleTest.java create mode 100644 src/test/java/de/juplo/kafka/wordcount/counter/SpringJunitConfigExampleTest.java 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 index 0000000..8088427 --- /dev/null +++ b/src/test/java/de/juplo/kafka/wordcount/counter/SpringBootTestExampleTest.java @@ -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 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 index 0000000..ae2b22e --- /dev/null +++ b/src/test/java/de/juplo/kafka/wordcount/counter/SpringJunitConfigExampleTest.java @@ -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()); + } +} -- 2.20.1