15

I am trying to write the unit test case for ListenableFuture adding Callback but I am not sure how to do it. Didn`t get anything useful on internet.

 @Test
    public void can_publish_data_to_kafka() {
        String topic = someString(10);
        String key = someAlphanumericString(5);
        String data = someString(50);
        SendResult sendResult = mock(SendResult.class);
        ListenableFuture<SendResult<String, Object>> future = mock(ListenableFuture.class);

        given(kafkaTemplate.send(topic, key, data)).willReturn(future);
        
        doAnswer(new Answer() {
            @Override
            public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
                return invocationOnMock.getArguments()[1];
            }
        });

        service.method(key, topic, data);

    }

Code for which i want to write test case

ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(topicName, key, data);

        future.addCallback(new ListenableFutureCallback<SendResult<String, Object>>() {
            @Override
            public void onSuccess(SendResult<String, Object> stringKafkaBeanSendResult) {
                RecordMetadata recordMetadata = stringKafkaBeanSendResult.getRecordMetadata();
                LOGGER.info(String.format("sent message %s to topic %s partition %s with offset %s" + data.toString(), recordMetadata.topic(), recordMetadata.partition(), recordMetadata.offset()));
            }

            @Override
            public void onFailure(Throwable throwable) {
                LOGGER.error(String.format("unable to send message = %s to topic %s because of error %s" + data.toString(), topicName, throwable.getMessage()));
            }
        });

I am expecting a direction in which i should go towards for writing UT using mockito.

Marcus K.
  • 980
  • 11
  • 26
David
  • 507
  • 2
  • 6
  • 14

2 Answers2

22

You can write a test case like this.

    @Test
    public void can_publishDataToKafka() {
        String key = someAlphanumericString();
        String topic = someAlphaString(10);
        long offset = somePositiveLong();
        int partition = somePositiveInteger();

        SiebelRecord siebelRecord = mock(SiebelRecord.class);
        SendResult<String, Object> sendResult = mock(SendResult.class);
        ListenableFuture<SendResult<String, Object>> responseFuture = mock(ListenableFuture.class);
        RecordMetadata recordMetadata = new RecordMetadata(new TopicPartition(topic, partition), offset, 0L, 0L, 0L, 0, 0);

        given(sendResult.getRecordMetadata()).willReturn(recordMetadata);
        when(kafkaTemplate.send(topic, key, siebelRecord)).thenReturn(responseFuture);
        doAnswer(invocationOnMock -> {
            ListenableFutureCallback listenableFutureCallback = invocationOnMock.getArgument(0);
            listenableFutureCallback.onSuccess(sendResult);
            assertEquals(sendResult.getRecordMetadata().offset(), offset);
            assertEquals(sendResult.getRecordMetadata().partition(), partition);
            return null;
        }).when(responseFuture).addCallback(any(ListenableFutureCallback.class));

        service.publishDataToKafka(key, topic, siebelRecord);

        verify(kafkaTemplate, times(1)).send(topic, key, siebelRecord);
    }

    @Test(expected = KafkaException.class)
    public void can_capture_failure_publishDataToKafka() {
        String key = someAlphanumericString();
        String topic = someAlphaString(10);
        String message = someString(20);

        SiebelRecord siebelRecord = mock(SiebelRecord.class);
        ListenableFuture<SendResult<String, Object>> responseFuture = mock(ListenableFuture.class);
        Throwable throwable = mock(Throwable.class);

        given(throwable.getMessage()).willReturn(message);
        when(kafkaTemplate.send(topic, key, siebelRecord)).thenReturn(responseFuture);
        doAnswer(invocationOnMock -> {
            ListenableFutureCallback listenableFutureCallback = invocationOnMock.getArgument(0);
            listenableFutureCallback.onFailure(throwable);
            return null;
        }).when(responseFuture).addCallback(any(ListenableFutureCallback.class));

        service.publishDataToKafka(key, topic, siebelRecord);
    }

Sahil
  • 613
  • 10
  • 16
  • How can I test this if I use Futures.addCallback. – Aditya Sharma May 01 '20 at 22:21
  • And I don't want to use PowerMockito. – Aditya Sharma May 01 '20 at 22:28
  • @AdityaSharma Check if it is helpful `ListeningScheduledExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadScheduledExecutor()); ListenableFuture futureResponse = executor.schedule(call, n, unit);` – Ronak Poriya Jun 09 '20 at 15:25
  • I am not sure why, but the doAnswer block has no effect and does not get executed. – al gh Feb 03 '21 at 05:16
  • can_capture_failure doesn't throw a KafkaException for some reason. I can see it is activating the onFailure callback, but test fails with Expected exception:... – Granfaloon Mar 03 '21 at 17:31
0

Given the following main class:

public class KafkaTemplateUnitTestExample {

    private final KafkaTemplate<String, Object> kafkaTemplate;
    private final Logger logger;

    public KafkaTemplateUnitTestExample(
            final KafkaTemplate<String, Object> kafkaTemplate,
            final Logger logger) {
        this.kafkaTemplate = kafkaTemplate;
        this.logger = logger;
    }

    public void method(String topicName, String key, Object data) {
        ListenableFuture<SendResult<String, Object>> future = kafkaTemplate.send(topicName, key, data);

        future.addCallback(stringKafkaBeanSendResult -> {
            RecordMetadata recordMetadata = stringKafkaBeanSendResult.getRecordMetadata();
            logger.info(String.format("sent message %s to topic %s partition %s with offset %s", data.toString(), recordMetadata.topic(), recordMetadata.partition(), recordMetadata.offset()));
        }, ex -> {
            logger.error(String.format("unable to send message = %s to topic %s because of error %s", data.toString(), topicName, ex.getMessage()));
        });
    }
}

Then you can test the ListenableFuture callbacks with Mockito like this:

@ExtendWith(MockitoExtension.class)
class KafkaTemplateUnitTestExampleTest {

    @Mock
    KafkaTemplate<String, Object> kafkaTemplate;
    @Mock
    Logger logger;
    @InjectMocks
    KafkaTemplateUnitTestExample service;
    private CompletableFuture<SendResult<String, Object>> completableFuture;

    @BeforeEach
    void setUp() {
        completableFuture = new CompletableFuture<>();
        var futureAdapter = new CompletableToListenableFutureAdapter<>(completableFuture);
        when(kafkaTemplate.send(any(), any(), any())).thenReturn(futureAdapter);
    }

    @Test
    void testSuccessCallback() {
        String topic = someString(10);
        String key = someAlphanumericString(5);
        String data = someString(50);

        service.method(key, topic, data);
        completableFuture.complete(new SendResult<>(
                new ProducerRecord<>("myTopic", "result"),
                new RecordMetadata(new TopicPartition("myTopic", 1), 42, 23, 123456, 4, 8)
        ));

        verify(logger).info(anyString());
    }

    @Test
    void testFailureCallback() {
        String topic = someString(10);
        String key = someAlphanumericString(5);
        String data = someString(50);

        service.method(key, topic, data);
        completableFuture.completeExceptionally(new RuntimeException("testEx"));

        verify(logger).error(anyString());
    }

   
    // missing String factories

}

This is in my opinion a cleaner way of testing the callbacks. You must be able to mock your Logger for this solution, but that hopefully shouldn't be a problem.

Marcus K.
  • 980
  • 11
  • 26