junit断言_JUnit断言

本文介绍了JUnit Jupiter提供的各种断言方法,如fail()、assertNull()、assertTrue()、assertEquals()等,帮助编写有效的测试代码。通过示例展示了如何使用这些断言,并解释了它们在测试过程中的作用。
摘要由CSDN通过智能技术生成

junit断言

JUnit Assertions allows us to write effective test methods. JUnit 5 is the latest version and JUnit Jupiter provides a lot of assertions to assert different types of statements.

JUnit断言允许我们编写有效的测试方法。 JUnit 5是最新版本,JUnit Jupiter提供了许多断言来断言不同类型的语句。

JUnit断言 (JUnit Assertions)

JUnit Jupiter org.junit.jupiter.api.Assertions class provides a collection of utility methods to use in our test code. All these methods are static, so we can import them and write fluent code. Almost all of these methods are overloaded to support primitives, objects, Collections, Stream, Arrays etc.

JUnit Jupiter org.junit.jupiter.api.Assertions类提供了可在我们的测试代码中使用的实用程序方法的集合。 所有这些方法都是静态的,因此我们可以导入它们并编写流畅的代码。 几乎所有这些方法都被重载以支持原语,对象,集合,流,数组等。

import static org.junit.jupiter.api.Assertions.*;

Let’s look at some of the important JUnit assert methods with an example.

让我们通过示例来看一些重要的JUnit assert方法。

失败() (fail())

This is used to fail a test, it’s useful when your test method is work in progress and you want to indicate that by fail-fast your test. There are many overloaded fail() methods, let’s look at some of them.

这用于使测试失败,当您的测试方法正在开发中并且您想通过快速失败测试来表明这一点时,这很有用。 有很多重载的fail()方法,让我们看一下其中的一些方法。

@Test
@DisplayName("This will Fail, don't worry!")
void test_fail() {
	fail();
	fail("Not yet implemented");
	fail(() -> {
		return "Not yet implemented";
	});
	fail("Not Yet Implemented", new RuntimeException("Explicitly Failed"));
	fail(new RuntimeException("Explicitly Failed"));

}

We can provide custom failure message and specify the cause of failure.

我们可以提供自定义失败消息并指定失败原因。

assertNull()和assertNotNull() (assertNull() and assertNotNull())

These methods are used to check if the specified object is null or not. We can also specify custom failure message.

这些方法用于检查指定的对象是否为null。 我们还可以指定自定义失败消息。

@Test
@DisplayName("assertNull Examples")
void test_assertNull() {
	assertNull(null);
	//assertNull(new Object(), "assertNull Fail Message");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值