简体   繁体   中英

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

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

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: ..."

This assertion fails, since send was actually called with different arguments.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM