1 package de.juplo.kafka;
3 import lombok.extern.slf4j.Slf4j;
4 import org.apache.kafka.clients.consumer.ConsumerRecord;
5 import org.junit.jupiter.api.*;
6 import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
8 import org.springframework.boot.test.context.SpringBootTest;
9 import org.springframework.boot.test.context.TestConfiguration;
10 import org.springframework.context.annotation.Bean;
11 import org.springframework.http.MediaType;
12 import org.springframework.kafka.annotation.KafkaListener;
13 import org.springframework.kafka.test.context.EmbeddedKafka;
14 import org.springframework.test.web.servlet.MockMvc;
16 import java.time.Duration;
17 import java.util.LinkedList;
18 import java.util.List;
20 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
21 import static de.juplo.kafka.ApplicationTests.TOPIC;
22 import static org.awaitility.Awaitility.*;
23 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
24 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
29 "spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}",
30 "sumup.gateway.topic=" + TOPIC})
32 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
34 public class ApplicationTests
36 static final String TOPIC = "FOO";
37 static final int PARTITIONS = 10;
48 consumer.received.clear();
53 void testSendMessage() throws Exception
56 .perform(post("/peter")
58 .contentType(MediaType.APPLICATION_JSON))
59 .andExpect(status().isOk());
60 await("Message was send")
61 .atMost(Duration.ofSeconds(5))
62 .until(() -> consumer.received.size() == 1);
68 final List<ConsumerRecord<String, String>> received = new LinkedList<>();
70 @KafkaListener(groupId = "TEST", topics = TOPIC)
71 public void receive(ConsumerRecord<String, String> record)
73 log.debug("Received message: {}", record);
79 static class Configuration
84 return new Consumer();