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.junit.jupiter.api.extension.ExtendWith;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.beans.factory.annotation.Value;
9 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
10 import org.springframework.boot.test.context.SpringBootTest;
11 import org.springframework.boot.test.context.TestConfiguration;
12 import org.springframework.context.annotation.Bean;
13 import org.springframework.kafka.annotation.KafkaListener;
14 import org.springframework.kafka.test.context.EmbeddedKafka;
15 import org.springframework.test.context.junit.jupiter.SpringExtension;
16 import org.springframework.test.web.servlet.MockMvc;
18 import java.time.Duration;
19 import java.util.LinkedList;
20 import java.util.List;
22 import static de.juplo.kafka.ApplicationTests.PARTITIONS;
23 import static de.juplo.kafka.ApplicationTests.TOPIC;
24 import static org.awaitility.Awaitility.*;
25 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
26 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
27 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30 @ExtendWith(SpringExtension.class)
31 @EmbeddedKafka(topics = TOPIC, partitions = PARTITIONS)
33 public class ApplicationTests
35 static final String TOPIC = "FOO";
36 static final int PARTITIONS = 10;
38 @Value("${spring.embedded.kafka.brokers}")
39 String bootstrapServers;
42 void testConcurrentProducers() throws Exception
44 SimpleProducer instanceA = new SimpleProducer(
48 SimpleProducer instanceB = new SimpleProducer(
55 .perform(post("/peter").content("Hallo Welt!"))
56 .andExpect(status().isOk());
57 await("Message was send")
58 .atMost(Duration.ofSeconds(5))
59 .until(() -> consumer.received.size() == 1);
63 void testSendFooMessage() throws Exception
66 .perform(put("/peter"))
67 .andExpect(status().isOk());
68 await("Message was send")
69 .atMost(Duration.ofSeconds(5))
70 .until(() -> consumer.received.size() == 1);
74 void testSendGreeting() throws Exception
77 .perform(post("/").content("peter"))
78 .andExpect(status().isOk());
79 await("Message was send")
80 .atMost(Duration.ofSeconds(5))
81 .until(() -> consumer.received.size() == 1);