JUnit - 使用错误的断言

发布于 2024-08-03 21:29:52 字数 865 浏览 3 评论 0原文

我引用 exubero 条目中的这篇文章。我认为这篇文章将使每个进行单元测试的人受益:

Junit 的 Assert 类中定义了许多以断言开头的不同方法。这些方法中的每一种对于它们所断言的内容都有稍微不同的参数和语义。

下面展示了assertTrue的一些不规则使用:

assertTrue("Objects must be the same", expected == actual);
assertTrue("Objects must be equal", expected.equals(actual));
assertTrue("Object must be null", actual == null);
assertTrue("Object must not be null", actual != null);

一些单元测试专家指出,上面的代码可以更好地写成:

assertSame("Objects must be the same", expected, actual);
assertEquals("Objects must be equal", expected, actual);
assertNull("Object must be null", actual);
assertNotNull("Object must not be null", actual);

使用适当的'assertXXX()'的好处之一是增加单元测试的可读性。谁能指出使用适当的 'assertXXX()' 还有什么其他好处?

I quote this post from exubero's entry. I think this entry will benefit everyone who is doing a unit test:

There are a large number of different methods beginning with assert defined in Junit's Assert class. Each of these methods has slightly different arguments and semantics about what they are asserting.

The following shows some irregular uses of assertTrue:

assertTrue("Objects must be the same", expected == actual);
assertTrue("Objects must be equal", expected.equals(actual));
assertTrue("Object must be null", actual == null);
assertTrue("Object must not be null", actual != null);

Some unit testing experts pointed out that the above code could be better written as:

assertSame("Objects must be the same", expected, actual);
assertEquals("Objects must be equal", expected, actual);
assertNull("Object must be null", actual);
assertNotNull("Object must not be null", actual);

One of the advantage of using the appropriate 'assertXXX()' will increase the readability of the unit test. Can anyone point out what other benefit of using the appropriate 'assertXXX()' ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

断桥再见 2024-08-10 21:29:52

我不是 Java 开发人员,我不知道断言失败时 JUnit 会输出什么。
我一直在使用的许多单元测试框架在使用像assertEquals这样的东西时会输出更好的错误信息。

让我向您展示我正在谈论的一个示例:

assertTrue("Objects must be equal", "One" == "Two");
assertEquals("Objects must be equal", "One", "Two");

在第一种情况下,您可能会得到如下错误输出:

错误:预期为真,实​​际为假。

第二种情况的输出:

错误:执行的“一”实际为“二”。

正如您所看到的,第二种情况提供了更好、更有意义的信息。

I'm not a Java developer and I don't know what JUnit outputs when an assertion fails.
Many unit testing frameworks that I've been using output better error information when use something like assertEquals.

Let me show you an example what I'm talking about:

assertTrue("Objects must be equal", "One" == "Two");
assertEquals("Objects must be equal", "One", "Two");

In first case you can have an error output like this:

Error: Expected true actual was false.

Output for the second case:

Error: Exected "One" actual was "Two".

As you can see the second case gives better more meaningful information.

再浓的妆也掩不了殇 2024-08-10 21:29:52

除了上面@Vadim 所说的之外,使用正确的断言可以防止由于测试的剪切复制粘贴而产生的错误。

作为一个例子

assertTrue("Objects must not be the same", expected != actual);

,然后复制并修改为

assertTrue("Objects must not be the same", newobject == actual);

当代码更改并且此测试失败并且注释欺骗下一个开发人员以引入新错误的方式“修复”代码时。

如果剪切-复制-粘贴代码是这样的:

assertFalse("Objects must be the same", newobject == actual);

注释、断言和测试用例的不一致可能会更明显。

是的,我已经看到这种情况发生了。

In addition to what @Vadim daid above, using the proper assert may guard against bugs created by cut-copy-paste of tests.

As an example

assertTrue("Objects must not be the same", expected != actual);

Is then copied and modified to

assertTrue("Objects must not be the same", newobject == actual);

When the code changes and this test fails and the comment fools the next developer to "fix" the code in a way that introduces a new bug.

If the cut-copy-paste-code was something like this:

assertFalse("Objects must be the same", newobject == actual);

The dis-congruity of the comment, the assertion and the test case may be more noticeable.

And yes, I've seen this happen.

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