Java/ JUnit - AssertTrue 与 AssertFalse

发布于 2024-09-09 16:44:35 字数 970 浏览 9 评论 0原文

我对 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 时,assertTrueassertFalse 都会显示字符串。如果是这样,两者兼得还有什么意义呢?

编辑:我想我明白是什么让我困惑了。作者可能只是为了展示它们的功能而将它们放在一起(毕竟这是一个教程)。他设置了一个会失败的消息,这样消息就会打印出来并告诉我为什么失败。开始变得更有意义......我认为这就是解释,但我不确定。

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 技术交流群。

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

发布评论

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

评论(7

小镇女孩 2024-09-16 16:44:35

如果第二个参数的计算结果为 false(换句话说,它确保该值为 true),则 assertTrue 将失败。 assertFalse 则相反。

assertTrue("This will succeed.", true);
assertTrue("This will fail!", false);

assertFalse("This will succeed.", false);
assertFalse("This will fail!", true);

与许多其他事情一样,熟悉这些方法的最佳方法就是进行实验:-)。

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 :-).

┾廆蒐ゝ 2024-09-16 16:44:35

您的理解不正确,在这种情况下,请始终参考 JavaDoc

断言错误

public static void assertFalse(java.lang.String 消息,
                               布尔条件)

断言条件为假。如果不是,它会抛出一个带有给定消息的断言错误。

参数:

  • message - AssertionError 的标识消息(可以为空)
  • 条件 - 要检查的条件

Your understanding is incorrect, in cases like these always consult the JavaDoc.

assertFalse

public static void assertFalse(java.lang.String message,
                               boolean condition)

Asserts that a condition is false. If it isn't it throws an AssertionError with the given message.

Parameters:

  • message - the identifying message for the AssertionError (null okay)
  • condition - condition to be checked
俏︾媚 2024-09-16 16:44:35

重点是语义。在assertTrue 中,您断言表达式为真。如果不是,那么它将显示该消息并且断言​​将失败。在assertFalse 中,您断言表达式的计算结果为 false。如果不是,则会显示该消息并且断言​​失败。

assertTrue (message, value == false) == assertFalse (message, value);

它们在功能上是相同的,但如果您期望值为 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.

assertTrue (message, value == false) == assertFalse (message, value);

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 use assertTrue.

高速公鹿 2024-09-16 16:44:35

我认为这只是为了您的方便(以及代码的读者)

您的代码和单元测试应该理想地自我记录,这个 API 可以帮助您,

想想什么更容易阅读:

AssertTrue(!(a > 3));

或者

AssertFalse(a > 3);

当您在 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:

AssertTrue(!(a > 3));

or

AssertFalse(a > 3);

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 :)

最好是你 2024-09-16 16:44:35

我对你对这些方法的第一反应很感兴趣。我将在以后的论证中使用它来证明assertTrue 和assertFalse 都不是最友好的工具。如果您愿意使用

assertThat(thisOrThat, is(false));

它,它的可读性会更高,并且它也会打印更好的错误消息。

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

assertThat(thisOrThat, is(false));

it is much more readable, and it prints a better error message too.

温暖的光 2024-09-16 16:44:35

如果检查的值为 false,则 assertTrue 将失败,而 assertFalse 将执行相反的操作:如果检查的值为 true,则失败。

另一件事,你的最后一个assertEquals很可能会失败,因为它会将“Book was已经签出”字符串与m1.checkOut(b1,p2)的输出进行比较。它需要第三个参数(用于检查相等性的第二个值)。

assertTrue will fail if the checked value is false, and assertFalse 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).

放手` 2024-09-16 16:44:35

该课程包含一个逻辑错误:

    assertTrue("Book check in failed", ml.checkIn(b1));

    assertFalse("Book was aleready checked in", ml.checkIn(b1));

在第一个断言中,我们期望签入返回True(因为签入成功)。如果失败,我们将打印一条消息,例如“图书签入失败。
现在,在第二个断言中,我们预计签入会失败,因为这本书已经在第一行中签入了。所以我们期望 checkIn 返回 False。如果由于某种原因签入返回 True(我们不希望如此),则消息不应该是“图书已签入”,因为签入已成功。

The course contains a logical error:

    assertTrue("Book check in failed", ml.checkIn(b1));

    assertFalse("Book was aleready checked in", ml.checkIn(b1));

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.

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