|
| 1 | +package com.baeldung.kafka.testcontainers; |
| 2 | + |
| 3 | +import static org.hamcrest.CoreMatchers.containsString; |
| 4 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 5 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.concurrent.TimeUnit; |
| 10 | + |
| 11 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 12 | +import org.apache.kafka.clients.producer.ProducerConfig; |
| 13 | +import org.apache.kafka.common.serialization.StringDeserializer; |
| 14 | +import org.apache.kafka.common.serialization.StringSerializer; |
| 15 | +import org.junit.ClassRule; |
| 16 | +import org.junit.Test; |
| 17 | +import org.junit.runner.RunWith; |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.beans.factory.annotation.Value; |
| 20 | +import org.springframework.boot.test.context.SpringBootTest; |
| 21 | +import org.springframework.boot.test.context.TestConfiguration; |
| 22 | +import org.springframework.context.annotation.Bean; |
| 23 | +import org.springframework.context.annotation.Import; |
| 24 | +import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory; |
| 25 | +import org.springframework.kafka.core.ConsumerFactory; |
| 26 | +import org.springframework.kafka.core.DefaultKafkaConsumerFactory; |
| 27 | +import org.springframework.kafka.core.DefaultKafkaProducerFactory; |
| 28 | +import org.springframework.kafka.core.KafkaTemplate; |
| 29 | +import org.springframework.kafka.core.ProducerFactory; |
| 30 | +import org.springframework.test.annotation.DirtiesContext; |
| 31 | +import org.springframework.test.context.junit4.SpringRunner; |
| 32 | +import org.testcontainers.containers.KafkaContainer; |
| 33 | +import org.testcontainers.utility.DockerImageName; |
| 34 | + |
| 35 | +import com.baeldung.kafka.embedded.KafkaConsumer; |
| 36 | +import com.baeldung.kafka.embedded.KafkaProducer; |
| 37 | +import com.baeldung.kafka.embedded.KafkaProducerConsumerApplication; |
| 38 | + |
| 39 | +@RunWith(SpringRunner.class) |
| 40 | +@Import(com.baeldung.kafka.testcontainers.KafkaTestContainersIntegrationTest.KafkaTestContainersConfiguration.class) |
| 41 | +@SpringBootTest(classes = KafkaProducerConsumerApplication.class) |
| 42 | +@DirtiesContext |
| 43 | +public class KafkaTestContainersIntegrationTest { |
| 44 | + |
| 45 | + @ClassRule |
| 46 | + public static KafkaContainer kafka = new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:5.4.3")); |
| 47 | + |
| 48 | + @Autowired |
| 49 | + public KafkaTemplate<String, String> template; |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private KafkaConsumer consumer; |
| 53 | + |
| 54 | + @Autowired |
| 55 | + private KafkaProducer producer; |
| 56 | + |
| 57 | + @Value("${test.topic}") |
| 58 | + private String topic; |
| 59 | + |
| 60 | + @Test |
| 61 | + public void givenKafkaDockerContainer_whenSendingtoDefaultTemplate_thenMessageReceived() throws Exception { |
| 62 | + template.send(topic, "Sending with default template"); |
| 63 | + consumer.getLatch().await(10000, TimeUnit.MILLISECONDS); |
| 64 | + |
| 65 | + assertThat(consumer.getLatch().getCount(), equalTo(0L)); |
| 66 | + assertThat(consumer.getPayload(), containsString("embedded-test-topic")); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void givenKafkaDockerContainer_whenSendingtoSimpleProducer_thenMessageReceived() throws Exception { |
| 71 | + producer.send(topic, "Sending with own controller"); |
| 72 | + consumer.getLatch().await(10000, TimeUnit.MILLISECONDS); |
| 73 | + |
| 74 | + assertThat(consumer.getLatch().getCount(), equalTo(0L)); |
| 75 | + assertThat(consumer.getPayload(), containsString("embedded-test-topic")); |
| 76 | + } |
| 77 | + |
| 78 | + @TestConfiguration |
| 79 | + static class KafkaTestContainersConfiguration { |
| 80 | + |
| 81 | + @Bean |
| 82 | + ConcurrentKafkaListenerContainerFactory<Integer, String> kafkaListenerContainerFactory() { |
| 83 | + ConcurrentKafkaListenerContainerFactory<Integer, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); |
| 84 | + factory.setConsumerFactory(consumerFactory()); |
| 85 | + return factory; |
| 86 | + } |
| 87 | + |
| 88 | + @Bean |
| 89 | + public ConsumerFactory<Integer, String> consumerFactory() { |
| 90 | + return new DefaultKafkaConsumerFactory<>(consumerConfigs()); |
| 91 | + } |
| 92 | + |
| 93 | + @Bean |
| 94 | + public Map<String, Object> consumerConfigs() { |
| 95 | + Map<String, Object> props = new HashMap<>(); |
| 96 | + props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers()); |
| 97 | + props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); |
| 98 | + props.put(ConsumerConfig.GROUP_ID_CONFIG, "baeldung"); |
| 99 | + props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| 100 | + props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
| 101 | + return props; |
| 102 | + } |
| 103 | + |
| 104 | + @Bean |
| 105 | + public ProducerFactory<String, String> producerFactory() { |
| 106 | + Map<String, Object> configProps = new HashMap<>(); |
| 107 | + configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafka.getBootstrapServers()); |
| 108 | + configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); |
| 109 | + configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); |
| 110 | + return new DefaultKafkaProducerFactory<>(configProps); |
| 111 | + } |
| 112 | + |
| 113 | + @Bean |
| 114 | + public KafkaTemplate<String, String> kafkaTemplate() { |
| 115 | + return new KafkaTemplate<>(producerFactory()); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments