如何使用 Mockito 在 Junit 测试用例中验证 logger.error(字符串消息,可抛出的 t)



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

     @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("Could not publish to Kafka").equals(new Exception("Could not publish to 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);

应该工作

相关内容

  • 没有找到相关文章

最新更新