How can I test @onFailure method?

726 views Asked by At

How could I go to "@Override public void onFailure(Throwable ex) { ... }"?

It always go "@Override public void onSuccess(SendResult<String, KafkaPresponseDto> result) {...}.

I want to print log.error("Unable to send message=["+kafkaPgResponseDto.toString()+"] due to : " + ex.getMessage());

Please help...

ListenableFuture<SendResult<String, KafkaPgResponseDto>> future = pgResponseKafkaTemplate.send(kurlyTopicNamePgResponse, kafkaPgResponseDto);
        future.addCallback(new ListenableFutureCallback<SendResult<String, KafkaPgResponseDto>>(){
            @Override
            public void onSuccess(SendResult<String, KafkaPgResponseDto> result) {
                KafkaPgResponseDto kafkaPgResponseDto = result.getProducerRecord().value();
                log.debug("Send message=["+kafkaPgResponseDto.toString()+"] with offset=["+result.getRecordMetadata().offset()+"]");
            }
            @Override
            public void onFailure(Throwable ex) {
                log.error("Unable to send message=["+kafkaPgResponseDto.toString()+"] due to : "+ex.getMessage());
                kafkaTransactionService.failedProcessingKafkaHistorySave(orderNo, kurlyTopicNamePgResponse, gson.toJson(payload), ex.toString());
            }
        });
1

There are 1 answers

0
Artem Bilan On

I believe there is no need in the real Kafka to test your functionality. Consider to use a MockProducer for injection into that KafkaTemplate and emulate an error for that onFailure() case: https://www.baeldung.com/kafka-mockproducer