[英]Mockito.verify with KafkaProducer and ProducerRecord
I'm unit testing a very simple wrapper class for a KafkaProducer whose send method is simply like this 我正在为KafkaProducer的一个非常简单的包装器类进行单元测试,该包装器的send方法就是这样
public class EntityProducer {
private final KafkaProducer<byte[], byte[]> kafkaProducer;
private final String topic;
EntityProducer(KafkaProducer<byte[], byte[]> kafkaProducer, String topic)
{
this.kafkaProducer = kafkaProducer;
this.topic = topic;
}
public void send(String id, BusinessEntity entity) throws Exception
{
ProducerRecord<byte[], byte[]> record = new ProducerRecord<>(
this.topic,
Transformer.HexStringToByteArray(id),
entity.serialize()
);
kafkaProducer.send(record);
kafkaProducer.flush();
}
}
The unit test reads as follows: 单元测试的内容如下:
@Test public void send() throws Exception
{
@SuppressWarnings("unchecked")
KafkaProducer<byte[], byte[]> mockKafkaProducer = Mockito.mock(KafkaProducer.class);
String topic = "mock topic";
EntityProducer producer = new EntityProducer(mockKafkaProducer, topic);
BusinessEntitiy mockedEntity = Mockito.mock(BusinessEntity.class);
byte[] serialized = new byte[]{1,2,3};
when(mockedCipMsg.serialize()).thenReturn(serialized);
String id = "B441B675-294E-4C25-A4B1-122CD3A60DD2";
producer.send(id, mockedEntity);
verify(mockKafkaProducer).send(
new ProducerRecord<>(
topic,
Transformer.HexStringToByteArray(id),
mockedEntity.serialize()
)
);
verify(mockKafkaProducer).flush();
The first verify method fails, hence the test failis, with the following message: 第一种验证方法失败,因此测试失败,并显示以下消息:
Argument(s) are different! Wanted:
kafkaProducer.send(
ProducerRecord(topic=mock topic, partition=null, key=[B@181e731e, value=[B@35645047, timestamp=null)
);
-> at xxx.EntityProducerTest.send(EntityProducerTest.java:33)
Actual invocation has different arguments:
kafkaProducer.send(
ProducerRecord(topic=mock topic, partition=null, key=[B@6f44a157, value=[B@35645047, timestamp=null)
);
What is most relevant is that the key of the ProducerRecord is not the same, the value appears the same 最相关的是ProducerRecord的键不相同,值看起来相同
Is the unit test properly oriented? 单元测试的方向是否正确? How may I make the test pass?
我怎样才能通过考试?
Kind regards. 亲切的问候。
This code: 这段代码:
verify(mockKafkaProducer).send(
new ProducerRecord<>(
topic,
Transformer.HexStringToByteArray(id),
mockedEntity.serialize()
)
);
Means: 手段:
"Verify that ' send ' was called on ' mockKafkaProducer ' with the following arguments: ..." “确认‘ 发送 ’被称为在‘mockKafkaProducer’使用以下参数:......”
This assertion fails, since send was actually called with different arguments. 该声明失败,因为实际上使用不同的参数调用了send。
I would suggest to capture the argument and verify it. 我建议捕获论点并进行验证。 Please see the code below:
请参见下面的代码:
ArgumentCaptor<ProducerRecord> captor = ArgumentCaptor.forClass(ProducerRecord.class);
verify(mockKafkaProducer).send(captor.capture());
ProducerRecord actualRecord = captor.getValue();
assertThat(actualRecord.topic()).isEqualTo("mock topic");
assertThat(actualRecord.key()).isEqualTo("...");
...
This is more readable (my view) and it is kind of document to what is happening in the method 这更具可读性(我的观点),是该方法正在发生的一种文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.