在使用Kafka绑定测试Spring Cloud Stream时遇到问题

时间:2019-09-16 17:22:07

标签: java apache-kafka spring-cloud-stream

我有以下绑定

public interface KafkaBinding {
    String DATA_OUT = "dataout";

    @Output(DATA_OUT)
    MessageChannel dataOut();
}

这是kafka实用程序

     @EnableBinding(KafkaBinding.class)
        public class KafkaStreamUtil {



 private final MessageChannel out;

        public KafkaStreamUtil(KafkaBinding binding) {
            this.out = binding .dataOut();
        }
        public void SendToKafkaTopic(List<data> dataList){


   dataList
            .stream()
            .forEach(this::sendMessgae);
}

private void sendMessgae(Data data) {
    Message<Data> message = MessageBuilder
            .withPayload(data)
            .setHeader(KafkaHeaders.MESSAGE_KEY, data.getRequestId().getBytes())
            .build();
    try {
        this.out.send(message);

    } catch (Exception e) {
        log.error(e.getMessage(),e);
    }

}

我的测试班

            @RunWith(SpringRunner.class)
            @ActiveProfiles("test")
            @SpringBootTest(classes = {KafkaStreamUtil.class,KafkaBinding.class})
            @DirtiesContext(classMode= DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
            public class KafkaStreamUtilTest {



                private static final String TEST_TOPIC1 = "data";
    private static final String GROUP_NAME = "embeddedKafkaApplication";

    @ClassRule
    public static EmbeddedKafkaRule kafkaRule =
            new EmbeddedKafkaRule(1, true, TEST_TOPIC1);

    public static EmbeddedKafkaBroker embeddedKafka = kafkaRule.getEmbeddedKafka();


    @Autowired
    private KafkaStreamUtil kUtil;

    @Autowired
    private KafkaBinding binding;


    @BeforeClass
    public static void setupProperties() {
        System.setProperty("spring.cloud.stream.kafka.binder.brokers", embeddedKafka.getBrokersAsString());

        System.setProperty("spring.cloud.stream.kafka.streams.binder.configuration.commit.interval.ms", "1000");
        System.setProperty("spring.cloud.stream.kafka.streams.binder.configuration.default.key.serde", "org.apache.kafka.common.serialization.Serdes$StringSerde");
        System.setProperty("spring.cloud.stream.kafka.streams.binder.configuration.default.value.serde", "org.apache.kafka.common.serialization.Serdes$StringSerde");

        System.setProperty("spring.cloud.stream.bindings.dataout.destination", "data");
        System.setProperty("spring.cloud.stream.bindings.dataout.producer.header-mode", "raw");

        System.setProperty("spring.autoconfigure.exclude","org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration");
        System.setProperty("spring.kafka.consumer.value-deserializer","org.apache.kafka.common.serialization.StringDeserializer");

        System.setProperty("spring.cloud.stream.bindings.input.consumer.headerMode","raw");
        System.setProperty("spring.cloud.stream.bindings.input.group","embeddedKafkaApplication");
        System.setProperty("spring.kafka.consumer.group-id","EmbeddedKafkaIntTest");



    }

    @Before
    public void setUp() throws Exception {
        kUtil = new KafkaStreamUtil(binding);
    }


    @Test
    public void sendToKafkaTopic() {


        kUtil.SendToKafkaTopic(dataList);


        Map<String, Object> consumerProps = KafkaTestUtils.consumerProps(GROUP_NAME, "false", embeddedKafka);
        consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        consumerProps.put("key.deserializer", StringDeserializer.class);
        consumerProps.put("value.deserializer", StringDeserializer.class);
        DefaultKafkaConsumerFactory<String, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);

        Consumer<String, String> consumer = cf.createConsumer();
        consumer.subscribe(Collections.singleton(TEST_TOPIC1));
        ConsumerRecords<String, String> records = consumer.poll(10_000);


        consumer.commitSync();
        consumer.close();
        Assert.assertNotNull(records);



    }


}

在测试中,我无法与Kafka util绑定,绑定始终为null。请让我知道我所缺少的。我可以使用springboottest进行测试,但是它正在加载所有我只想加载此测试所需的必要组件的bean。

0 个答案:

没有答案