java的assert语句可以让你指定一条消息吗?
似乎在断言失败时让断言显示消息可能很有用。
目前会抛出一个 AssertionError
,您可以为其指定自定义消息吗?
您能否展示执行此操作的示例机制(除了创建自己的异常类型并抛出它)?
Seems likes it might be useful to have the assert display a message when an assertion fails.
Currently an AssertionError
gets thrown, can you specify a custom message for it?
Can you show an example mechanism for doing this (other than creating your own exception type and throwing it)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您当然可以:
请参阅使用断言编程了解更多信息。
You certainly can:
See Programming with Assertions for more information.
我建议将条件放在括号中
想象一下,如果您遇到这样的代码...
不要忘记这与您在 JUnit 中编写的断言无关。
I'd recommend putting the conditional in brackets
Imagine if you came across code like this...
Don't forget this has nothing to do with asserts you'd write in JUnit.
确实如此:
这会将“重要的变量为空”添加到引发的异常中。
It absolutely does:
This will add "The important var was null" to the exception that is thrown.
如果使用
Expression2,则将其用作 AssertionError 的详细消息。
If you use
Expression2 is used as a detail message for the AssertionError.