Java/ JUnit - AssertTrue 与 AssertFalse
我对 Java 还很陌生,正在关注 Eclipse Total 初学者教程。它们都非常有帮助,但在第 12 课中,他对一个测试用例使用 assertTrue
,对另一个测试用例使用 assertFalse
。这是代码:
// Check the book out to p1 (Thomas)
// Check to see that the book was successfully checked out to p1 (Thomas)
assertTrue("Book did not check out correctly", ml.checkOut(b1, p1)); // If checkOut fails, display message
assertEquals("Thomas", b1.getPerson().getName());
assertFalse("Book was already checked out", ml.checkOut(b1,p2)); // If checkOut fails, display message
assertEquals("Book was already checked out", m1.checkOut(b1,p2));
我已经搜索了有关这些方法的良好文档,但没有找到任何内容。如果我的理解是正确的,当第二个参数计算结果为 false 时,assertTrue
和 assertFalse
都会显示字符串。如果是这样,两者兼得还有什么意义呢?
编辑:我想我明白是什么让我困惑了。作者可能只是为了展示它们的功能而将它们放在一起(毕竟这是一个教程)。他设置了一个会失败的消息,这样消息就会打印出来并告诉我为什么失败。开始变得更有意义......我认为这就是解释,但我不确定。
I'm pretty new to Java and am following the Eclipse Total Beginner's Tutorials. They are all very helpful, but in Lesson 12, he uses assertTrue
for one test case and assertFalse
for another. Here's the code:
// Check the book out to p1 (Thomas)
// Check to see that the book was successfully checked out to p1 (Thomas)
assertTrue("Book did not check out correctly", ml.checkOut(b1, p1)); // If checkOut fails, display message
assertEquals("Thomas", b1.getPerson().getName());
assertFalse("Book was already checked out", ml.checkOut(b1,p2)); // If checkOut fails, display message
assertEquals("Book was already checked out", m1.checkOut(b1,p2));
I have searched for good documentation on these methods, but haven't found anything. If my understanding is correct, assertTrue
as well as assertFalse
display the string when the second parameter evaluates to false. If so, what is the point of having both of them?
Edit: I think I see what was confusing me. The author may have put both of them in just to show their functionality (it IS a tutorial after all). And he set up one which would fail, so that the message would print out and tell me WHY it failed. Starting to make more sense...I think that's the explanation, but I'm not sure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果第二个参数的计算结果为 false(换句话说,它确保该值为 true),则
assertTrue
将失败。assertFalse
则相反。与许多其他事情一样,熟悉这些方法的最佳方法就是进行实验:-)。
assertTrue
will fail if the second parameter evaluates tofalse
(in other words, it ensures that the value is true).assertFalse
does the opposite.As with many other things, the best way to become familiar with these methods is to just experiment :-).
您的理解不正确,在这种情况下,请始终参考 JavaDoc。
Your understanding is incorrect, in cases like these always consult the JavaDoc.
重点是语义。在assertTrue 中,您断言表达式为真。如果不是,那么它将显示该消息并且断言将失败。在assertFalse 中,您断言表达式的计算结果为 false。如果不是,则会显示该消息并且断言失败。
它们在功能上是相同的,但如果您期望值为 false,则使用
assertFalse
。如果您期望值为 true,请使用assertTrue
。The point is semantics. In assertTrue, you are asserting that the expression is true. If it is not, then it will display the message and the assertion will fail. In assertFalse, you are asserting that an expression evaluates to false. If it is not, then the message is displayed and the assertion fails.
These are functionally the same, but if you are expecting a value to be false then use
assertFalse
. If you are expecting a value to be true, then useassertTrue
.我认为这只是为了您的方便(以及代码的读者)
您的代码和单元测试应该理想地自我记录,这个 API 可以帮助您,
想想什么更容易阅读:
或者
当您在 xx 之后打开测试时当你的测试突然失败几个月时,你花更少的时间来理解第二种情况出了什么问题(我的观点)。如果您不同意,您可以在所有情况下始终坚持使用 AssertTrue :)
I think it's just for your convenience (and the readers of your code)
Your code, and your unit tests should be ideally self documenting which this API helps with,
Think abt what is more clear to read:
or
When you open your tests after xx months when your tests suddenly fail, it would take you much less time to understand what went wrong in the second case (my opinion). If you disagree, you can always stick with AssertTrue for all cases :)
我对你对这些方法的第一反应很感兴趣。我将在以后的论证中使用它来证明assertTrue 和assertFalse 都不是最友好的工具。如果您愿意使用
它,它的可读性会更高,并且它也会打印更好的错误消息。
Your first reaction to these methods is quite interesting to me. I will use it in future arguments that both assertTrue and assertFalse are not the most friendly tools. If you would use
it is much more readable, and it prints a better error message too.
如果检查的值为 false,则
assertTrue
将失败,而assertFalse
将执行相反的操作:如果检查的值为 true,则失败。另一件事,你的最后一个assertEquals很可能会失败,因为它会将“Book was已经签出”字符串与m1.checkOut(b1,p2)的输出进行比较。它需要第三个参数(用于检查相等性的第二个值)。
assertTrue
will fail if the checked value is false, andassertFalse
will do the opposite: fail if the checked value is true.Another thing, your last assertEquals will very likely fail, as it will compare the "Book was already checked out" string with the output of m1.checkOut(b1,p2). It needs a third parameter (the second value to check for equality).
该课程包含一个逻辑错误:
在第一个断言中,我们期望签入返回True(因为签入成功)。如果失败,我们将打印一条消息,例如“图书签入失败。
现在,在第二个断言中,我们预计签入会失败,因为这本书已经在第一行中签入了。所以我们期望 checkIn 返回 False。如果由于某种原因签入返回 True(我们不希望如此),则消息不应该是“图书已签入”,因为签入已成功。
The course contains a logical error:
In the first assert we expect the checkIn to return True (because checkin is succesful). If this would fail we would print a message like "book check in failed.
Now in the second assert we expect the checkIn to fail, because the book was checked in already in the first line. So we expect a checkIn to return a False. If for some reason checkin returns a True (which we don't expect) than the message should never be "Book was already checked in", because the checkin was succesful.