我正在尝试对我的kafka消费者进行单元测试。我正在尝试使用MockConsumer
java api附带的kafka-client
类。
以下是我的配置代码
@Bean
public MockConsumer consumer(){
MockConsumer consumer = new MockConsumer(OffsetResetStrategy.LATEST);
consumer.assign(Arrays.asList(new TopicPartition("test-topic", 0)));
HashMap<TopicPartition, Long> beginningOffsets = new HashMap<>();
beginningOffsets.put(new TopicPartition("test-topic", 0), 0L);
consumer.updateBeginningOffsets(beginningOffsets);
consumer.addRecord(new ConsumerRecord<String, String>("test-topic",0,
0L, "mykey", "myvalue0"));
consumer.addRecord(new ConsumerRecord<String, String>("test-topic", 0,
1L, "mykey", "myvalue1"));
consumer.addRecord(new ConsumerRecord<String, String>("test-topic", 0,
2L, "mykey", "myvalue2"));
consumer.addRecord(new ConsumerRecord<String, String>("test-topic", 0,
3L, "mykey", "myvalue3"));
consumer.addRecord(new ConsumerRecord<String, String>("test-topic", 0,
4L, "mykey", "myvalue4"));
HashMap<TopicPartition, Long> endOffsets = new HashMap<>();
endOffsets.put(new TopicPartition("test-topic", 0), 4L);
consumer.updateEndOffsets(endOffsets);
return consumer;
}
现在当我在我的测试用例中使用这个MockConsumer Bean时,如下所示
@Autowired
MockConsumer kafkaConsumer;
@Autowired
@InjectMocks
MyConsumer myConsumer; //this is the class having consumer code. This
//is the class under test
@Test
public void testConsumeWithAutoAssignment() throws Exception {
myConsumer.consumeTopic("test-topic");
}
我从
获得例外 kafkaConsumer.subscribe(topic)
java.lang.IllegalStateException: Subscription to topics, partitions and pattern are mutually exclusive
如果有人发现了问题或解决了这个问题,请告诉我。
答案 0 :(得分:8)
这是因为在你使用consumer.assign(Arrays.asList(new TopicPartition("test-topic", 0)));
的bean中,这意味着消费者想要从&#34; test-topic&#34;中的特定分区(0)消费。然后在某个地方,但我不知道你提供的代码在哪里,可以打电话给subscribe(topic)
。使用subscribe,消费者成为消费者组的一部分,Kafka代理自动分配分区(用于重新平衡)。您不能同时使用它们:分配特定分区(USER DEFINED)和订阅自动分配。
答案 1 :(得分:-1)
我遇到了同样的问题,解决方法是将/one/{id}/two/
分配给变量并使用它。以下代码适用于我:
new TopicPartition(topic, 0)