为Kafka Consumer编写JUnit测试

时间:2018-06-19 05:16:45

标签: java apache-kafka

我有一个订阅主题的kafka消费者。实施工作正常。但是当试图为此实现单元测试时,由于它是由Runnable接口实现的,因此存在问题。

实施

@Override
public void run() {
    kafkaConsumer.subscribe(kafkaTopics);

    while (true) {
        ConsumerRecords<String, String> records = kafkaConsumer.poll(1000);
        Map<String, InventoryStock> skuMap = new LinkedHashMap<>();

        try {
            // populating sku map with consumer record
            for (ConsumerRecord<String, String> record : records) {
                populateMap(skuMap, record.value());
            }

            if (MapUtils.isNotEmpty(skuMap)) {
                // writing sku inventory with populated sku map
                inventoryDao.updateInventoryTable(INVENTORY_JOB_ID, skuMap);
            }
        } catch (Exception e) {

        }
        kafkaConsumer.commitAsync();
    }
}

我尝试使用MockConsumer实施测试。但它需要在实施中分配给消费者。但实施中的消费者并没有暴露出来。这是我尝试过的。

@Before
public void onBefore() {
    MockitoAnnotations.initMocks(this);

    Properties consumerProps = new Properties();
    consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "test-group");
    consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);

    consumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST);
    skuInventoryConsumer = new SkuInventoryConsumer(consumerProps);

    KafkaConsumer kafkaConsumerMock = mock(KafkaConsumer.class);

    Whitebox.setInternalState(skuInventoryConsumer, "LOGGER", LOGGER);
    Whitebox.setInternalState(skuInventoryConsumer, "kafkaConsumer", kafkaConsumerMock);

}

@Test
public void should_subscribe_on_topic() {
    consumer.assign(Arrays.asList(new TopicPartition("my_topic", 0)));

    HashMap<TopicPartition, Long> beginningOffsets = new HashMap<>();
    beginningOffsets.put(new TopicPartition("my_topic", 0), 0L);
    consumer.updateBeginningOffsets(beginningOffsets);

    consumer.addRecord(new ConsumerRecord<>("my_topic", 0, 0L, "mykey", "myvalue0"));
    consumer.addRecord(new ConsumerRecord<>("my_topic", 0, 1L, "mykey", "myvalue1"));
    consumer.addRecord(new ConsumerRecord<>("my_topic", 0, 2L, "mykey", "myvalue2"));
    consumer.addRecord(new ConsumerRecord<>("my_topic", 0, 3L, "mykey", "myvalue3"));
    consumer.addRecord(new ConsumerRecord<>("my_topic", 0, 4L, "mykey", "myvalue4"));
}

由于它是runnable并且消费者未被曝光,因此该测试未按预期工作。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我建议使用Mockito,就像下面的示例

    Consumer<String, String> kafkaConsumerLocal = mock(Consumer.class);
    KafkaConsumer kafkaConsumer = spy(new KafkaConsumer("topic-name));

    ReflectionTestUtils.setField(kafkaConsumer, "threadPoolCount", 1);
    ReflectionTestUtils.setField(kafkaConsumer, "consumer", kafkaConsumerLocal);

    doNothing().when(kafkaConsumer).runConsumer();
    doNothing().when(kafkaConsumer).addShutDownHook();
    doReturn(kafkaConsumerLocal).when(consumerInit).getKafkaConsumer();