简体   繁体   中英

How can I test @onFailure method?

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());
            }
        });

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

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