gpt4 book ai didi

java - 如何使用 Mockito 验证 Junit 测试用例中的 logger.error(String message, Throwable t)

转载 作者:行者123 更新时间:2023-11-30 05:48:16 26 4
gpt4 key购买 nike

我正在尝试测试Kafka回调onFailure方法:

     @Override
public void onFailure(Throwable ex) {
logger.error("Failure while sending message in kafka.", ex);
}

My test case code using Mockito. I am setting exception in object of class SettableListenableFuture.



SettableListenableFuture<SendResult<String, DeserializationResult<EnvelopeExpanded<SpecificRecord>>>> future2 = new SettableListenableFuture<>();
future2.setException(new Exception("Could not publish to Kafka"));
Exception ex = new Exception("Could not publish to Kafka");
verify(logger, times(1)).error("Failure while sending message in kafka.", ex);

我收到此错误

  Argument(s) are different! Wanted:
logger.error(
"Failure while sending message in kafka.",
java.lang.Exception: Could not publish to Kafka
);

Actual invocation has different arguments:
logger.error(
"Failure while sending message in kafka.",
java.lang.Exception: Could not publish to Kafka
);

想要的参数和实际的参数是相同的,但它仍然抛出错误。谁能帮我解决这个问题吗??

最佳答案

System.out.println(new Exception("无法发布到 Kafka").equals(new Exception("无法发布到 Kafka"))) 将打印 false

所以

   Exception ex = new Exception("Could not publish to Kafka");
SettableListenableFuture<...> future2 = new SettableListenableFuture<>();
future2.setException(ex);
verify(logger, times(1)).error("Failure while sending message in kafka.", ex);

应该可以

关于java - 如何使用 Mockito 验证 Junit 测试用例中的 logger.error(String message, Throwable t),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54456647/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com