问题描述
我是Java的新手,我正在关注。它们都非常有用,但在第12课中,他对一个测试用例使用 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));
我搜索了这些方法的好文档,但没有找到任何内容。如果我的理解是正确的, assertTrue
以及 assertFalse
在第二个参数求值为false时显示字符串。如果是这样的话,那么同时拥有它们有什么意义呢?
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?
编辑:我想我看到了令我困惑的事情。作者可能只是为了展示他们的功能而放置它们(毕竟它是一个教程)。他设置了一个会失败的信息,这样就会打印出来并告诉我为什么失败了。开始变得更有意义......我认为这是解释,但我不确定。
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.
推荐答案
assertTrue
如果第二个参数的计算结果为<$ c,则会失败$ c> false (换句话说,它确保值为true)。 assertFalse
则相反。
assertTrue
will fail if the second parameter evaluates to false
(in other words, it ensures that the value is true). assertFalse
does the opposite.
assertTrue("This will succeed.", true);
assertTrue("This will fail!", false);
assertFalse("This will succeed.", false);
assertFalse("This will fail!", true);
与许多其他事情一样,熟悉这些方法的最佳方法是尝试: - )。
As with many other things, the best way to become familiar with these methods is to just experiment :-).
这篇关于Java / JUnit - AssertTrue与AssertFalse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!