Assert That a Java Optional Has a Certain Value – 断言一个Java可选项目有一个特定的值

最后修改: 2021年 11月 15日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

When we’re testing methods that return Optional objects, we may need to write assertions that either check if the Optional has a value or check the value inside it.

当我们测试返回 Optional对象的方法时,我们可能需要编写断言来检查Optional是否有一个值,或者检查它里面的值。

In this short tutorial, we’ll look at how to write these assertions using functions from both JUnit and AssertJ.

在这个简短的教程中,我们将看看如何使用来自JUnitAssertJ的函数编写这些断言。

2. Test Whether an Optional Is Empty or Not

2.测试一个选项是否为空

If we only need to find out whether the Optional has a value, we can assert on isPresent or isEmpty.

如果我们只需要找出Optional是否有一个值,我们可以在isPresentisEmpty上断言。

2.1. Test Optional Has a Value

2.1.测试Optional有一个值

If an Optional has a value, we can assert on Optional.isPresent:

如果一个Optional有一个值,我们可以在Optional.isPresent上断言。

assertTrue(optional.isPresent());

However, the AssertJ library provides a more fluent way of expressing this:

然而,AssertJ库提供了一种更流畅的表达方式。

assertThat(optional).isNotEmpty();

2.2. Test Optional Is Empty

2.2.测试可选是空的

We can reverse the logic when using JUnit:

在使用JUnit时,我们可以把逻辑倒过来。

assertFalse(optional.isPresent());

Also, in Java 11 onwards, we can use Optional.isEmpty:

另外,在Java 11以后,我们可以使用Optional.isEmpty

assertTrue(optional.isEmpty());

However, AssertJ provides us a neat alternative too:

然而,AssertJ也为我们提供了一个巧妙的选择。

assertThat(optional).isEmpty();

3. Testing the Value of an Optional

3.测试一个选项的价值

Often we want to test the value inside an Optional rather than just presence or absence.

通常我们想测试Optional里面的值,而不仅仅是存在或不存在。

3.1. Using JUnit Assertions

3.1.使用JUnit断言

We can use Optional.get to provide the value and then write an assertion on that:

我们可以使用Optional.get来提供值,然后对其写一个断言

Optional<String> optional = Optional.of("SOMEVALUE");
assertEquals("SOMEVALUE", optional.get());

However, using get may cause an exception, which makes it harder to understand a test failure. So, we may prefer to assert whether the value is present first:

然而,使用get可能会引起异常,这使得测试失败更难理解。因此,我们可能更喜欢先断言该值是否存在:

assertTrue(optional.isPresent());
assertEquals("SOMEVALUE", optional.get());

However, Optional supports the equals method, so we can use an Optional with the correct value in it as part of a general equality assertion:

然而,Optional支持equals方法,所以我们可以使用一个包含正确值的Optional作为一般平等断言的一部分。

Optional<String> expected = Optional.of("SOMEVALUE");
Optional<String> actual = Optional.of("SOMEVALUE");
assertEquals(expected, actual);

3.2. Using AssertJ

3.2.使用AssertJ

With AssertJ, we can use the hasValue fluent assertion:

通过AssertJ我们可以使用hasValue流畅的断言

assertThat(Optional.of("SOMEVALUE")).hasValue("SOMEVALUE");

4. Conclusion

4.总结

In this article, we looked at a few ways to test Optional.

在这篇文章中,我们研究了测试Optional的几种方法。

We saw how the built-in JUnit assertions can be used with isPresent and get. We also saw how Optional.equals provides us a way to compare Optional objects in our assertions.

我们看到内置的JUnit断言如何与isPresentget一起使用。我们还看到了Optional.equals是如何为我们提供一种方法来比较我们断言中的Optional对象。

Finally, we looked at the AssertJ assertions, which give us fluent language to check our Optional values. 

最后,我们看了AssertJ断言,它给我们提供了流畅的语言来检查我们的Optional值。

As always, the code presented in this article is available over on GitHub.

一如既往,本文介绍的代码可在GitHub上获得over