gpt4 book ai didi

java - Junit ExpectedException 失败并显示解释性消息

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:08 28 4
gpt4 key购买 nike

在 junit 类中,我想测试是否抛出异常,如果没有,则提供一条解释性消息,提示在测试失败时要查找的内容。所以,像...

    thrown.expect(UnsupportedOperationException.class);
thrown.expectMessage("an exception message");
thrown.failureMessage("This method should not be called directly. Be sure to use a subclass or mock that implements the method foo()");

很明显,没有这个方法failureMessage()。我不可能是世界上唯一一个曾经想要实现失败消息的人。我在搜索中没有找到任何示例。

请注意,这是对 Junit4 @Rule 和 ExpectedException 功能的引用。

最佳答案

JUnit 4.12 支持自定义失败消息(参见 release notes)。如果您的测试没有抛出预期的异常,则使用此失败消息。其他一切,例如错误消息,都由 Hamcrest 匹配器处理。

JUnit 4.12 于 2014 年 12 月 4 日发布。

关于java - Junit ExpectedException 失败并显示解释性消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20251901/

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