java assert false,Java / JUnit - AssertTrue与AssertFalse

新手教程中,理解assertTrue与assertFalse在Java单元测试中的实际应用:assertTrue验证条件是否为真,assertFalse检查条件是否为假。两者结合演示了错误处理与测试用例设计。
摘要由CSDN通过智能技术生成

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.

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值