Java/JUnit - AssertTrue 与 AssertFalse

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3241105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:41:10  来源:igfitidea点击:

Java/ JUnit - AssertTrue vs AssertFalse

javajunitassertjunit4assertions

提问by Thomas

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 assertTruefor one test case and assertFalsefor another. Here's the code:

我对 Java 很陌生,正在关注Eclipse Total Beginner's Tutorials。它们都非常有帮助,但在第 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));

I have searched for good documentation on these methods, but haven't found anything. If my understanding is correct, assertTrueas well as assertFalsedisplay the string when the second parameter evaluates to false. If so, what is the point of having both of them?

我已经搜索了有关这些方法的良好文档,但没有找到任何内容。如果我的理解是正确的,assertTrue以及assertFalse当第二个参数计算为 false 时显示字符串。如果是这样,那么同时拥有它们又有什么意义呢?

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.

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

采纳答案by Matt Solnit

assertTruewill fail if the second parameter evaluates to false(in other words, it ensures that the value is true). assertFalsedoes the opposite.

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

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

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

回答by Thomas

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

您的理解是不正确的,在这种情况下,请务必查阅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

断言假

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

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

参数:

  • message- AssertionError 的识别消息(null OK)
  • condition- 要检查的条件

回答by bashflyng

assertTruewill fail if the checked value is false, and assertFalsewill do the opposite: fail if the checked value is true.

assertTrue如果检查的值为 false 将失败,并且assertFalse会做相反的事情:如果检查的值为 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).

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

回答by Xetius

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 中,您断言表达式为真。如果不是,那么它将显示消息并且断言​​将失败。在 assertFalse 中,您断言表达式的计算结果为 false。如果不是,则显示消息并且断言​​失败。

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

These are functionally the same, but if you are expecting a value to be falsethen use assertFalse. If you are expecting a value to be true, then use assertTrue.

这些在功能上是相同的,但是如果您期望值是false,则使用assertFalse. 如果您期望值为true,则使用assertTrue.

回答by tdobek

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,

理想情况下,您的代码和单元测试应该是自记录此 API 所帮助的,

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

当您在 xx 个月后测试突然失败时打开测试时,您将花费更少的时间来了解第二种情况下出了什么问题(我的意见)。如果你不同意,你可以在所有情况下都坚持使用 AssertTrue :)

回答by iwein

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

你对这些方法的第一反应对我来说很有趣。我将在以后的论点中使用它,即 assertTrue 和 assertFalse 都不是最友好的工具。如果你会使用

assertThat(thisOrThat, is(false));

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

它更具可读性,并且打印出更好的错误信息。

回答by Ronald Mos

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.

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