如何使用 logger 通过断言打印消息

发布于 2025-01-20 22:04:32 字数 357 浏览 0 评论 0原文

我正在尝试在我的存储库中实现记录器,但在使用 Junit 实现记录器时遇到一些问题。

示例断言:

logger.info("Asserting the response.");
assertThat(response.statusCode())
                    .withFailMessage("The test failed with status code:" + response.statusCode())
                    .isEqualTo(200);

我想使用 logger.error() 函数代替 withFailMessage 但我似乎找不到任何方法。

I am trying to implement a logger in my repo and I am having some issues with implementing logger with Junit.

Sample assertion:

logger.info("Asserting the response.");
assertThat(response.statusCode())
                    .withFailMessage("The test failed with status code:" + response.statusCode())
                    .isEqualTo(200);

I want to use logger.error() function in place of withFailMessage but I can't seem to find any method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深居我梦 2025-01-27 22:04:32

标准断言(即,assertthat())的意思是立即使用assertionError失败。

如果您想在发生故障时具有自定义逻辑, soft soft sostertions 与“ noreflow noreferrer”> callback功能可能是您要寻找的东西。

您的示例将变成类似:

SoftAssertions softly = new SoftAssertions();
softly.setAfterAssertionErrorCollected(error -> logger.error("Assertion failed: {}", error));

softly.assertThat(response.statusCode())
      .isEqualTo(200);

Standard assertions (i.e., assertThat()) are meant to fail immediately with an AssertionError.

If you would like to have custom logic in case of failures, Soft Assertions together with the callback feature might be what you are looking for.

Your example would become something like:

SoftAssertions softly = new SoftAssertions();
softly.setAfterAssertionErrorCollected(error -> logger.error("Assertion failed: {}", error));

softly.assertThat(response.statusCode())
      .isEqualTo(200);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文