|
| 1 | +/* |
| 2 | + * Copyright 2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.test.utils; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 22 | +import org.apache.kafka.clients.consumer.KafkaConsumer; |
| 23 | +import org.apache.kafka.clients.producer.KafkaProducer; |
| 24 | +import org.apache.kafka.clients.producer.ProducerRecord; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import org.springframework.kafka.test.EmbeddedKafkaBroker; |
| 28 | +import org.springframework.kafka.test.context.EmbeddedKafka; |
| 29 | + |
| 30 | +/** |
| 31 | + * @author Gary Russell |
| 32 | + * @since 2.2.7 |
| 33 | + * |
| 34 | + */ |
| 35 | +@EmbeddedKafka(topics = { "singleTopic1", "singleTopic2" }) |
| 36 | +public class KafkaTestUtilsTests { |
| 37 | + |
| 38 | + @Test |
| 39 | + void testGetSingleWithMoreThatOneTopic(EmbeddedKafkaBroker broker) { |
| 40 | + Map<String, Object> producerProps = KafkaTestUtils.producerProps(broker); |
| 41 | + KafkaProducer<Integer, String> producer = new KafkaProducer<>(producerProps); |
| 42 | + producer.send(new ProducerRecord<>("singleTopic1", 1, "foo")); |
| 43 | + producer.send(new ProducerRecord<>("singleTopic2", 1, "foo")); |
| 44 | + producer.close(); |
| 45 | + Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("ktuTests", "false", broker); |
| 46 | + consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); |
| 47 | + KafkaConsumer<Integer, String> consumer = new KafkaConsumer<>(consumerProps); |
| 48 | + broker.consumeFromAllEmbeddedTopics(consumer); |
| 49 | + KafkaTestUtils.getSingleRecord(consumer, "singleTopic1"); |
| 50 | + KafkaTestUtils.getSingleRecord(consumer, "singleTopic2"); |
| 51 | + consumer.close(); |
| 52 | + } |
| 53 | + |
| 54 | +} |
0 commit comments