Java/JUnit - AssertTrue 与 AssertFalse [英] Java/ JUnit - AssertTrue vs AssertFalse

查看:34
本文介绍了Java/JUnit - AssertTrue 与 AssertFalse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 新手,正在关注 Eclipse Total 初学者教程.它们都非常有用,但在第 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));

我已经搜索了有关这些方法的良好文档,但没有找到任何内容.如果我的理解是正确的,assertTrueassertFalse 会在第二个参数的计算结果为 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 如果第二个参数的计算结果为 false 将失败(换句话说,它确保值为真).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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆