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
@