AssertJ for Guava – Guava的AssertJ

最后修改: 2016年 7月 10日

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

1. Overview

1.概述

This article focuses on AssertJ Guava-related assertions and is the second article from the AssertJ series. If you want to some general info about AssertJ, have a look at the first article in the series Introduction to AssertJ.

本文重点介绍AssertJ瓜瓦相关的断言,是AssertJ系列的第二篇文章。如果你想了解一些关于AssertJ的一般信息,请看该系列的第一篇文章Introduction to AssertJ

2. Maven Dependencies

2.Maven的依赖性

In order to use AssertJ with Guava, you need to add the following dependency to your pom.xml:

为了在Guava中使用AssertJ,你需要在你的pom.xml中添加以下依赖。

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-guava</artifactId>
    <version>3.0.0</version>
    <scope>test</scope>
</dependency>

You can find the latest version here.

你可以在这里找到最新版本

And note that since version 3.0.0, AssertJ Guava relies on Java 8 and AssertJ Core 3.x.

并注意到从3.0.0版本开始,AssertJ Guava依赖于Java 8AssertJ Core 3.x

3. Guava Assertions in Action

3.Guava Assertions in Action

AssertJ has custom assertions for Guava types: ByteSource, Multimap, Optional, Range, RangeMap and Table.

AssertJ为Guava类型提供了自定义断言。ByteSource, Multimap, Optional, Range, RangeMapTable

3.1. ByteSource Assertions

3.1.ByteSource断言

Let’s start off by creating two empty temporary files:

让我们从创建两个空的临时文件开始。

File temp1 = File.createTempFile("bael", "dung1");
File temp2 = File.createTempFile("bael", "dung2");

and creating ByteSource instances from them:

并从中创建ByteSource实例。

ByteSource byteSource1 = Files.asByteSource(temp1);
ByteSource byteSource2 = Files.asByteSource(temp2);

Now we can write the following assertion:

现在我们可以写出以下断言。

assertThat(buteSource1)
  .hasSize(0)
  .hasSameContentAs(byteSource2);

3.2. Multimap Assertions

3.2.Multimap断言

Multimaps are maps that can associate more than one value with a given key. The Multimap assertions work pretty similarly to normal Map implementations.

Multimaps是可以将一个以上的值与给定的键相关联的地图。Multimap断言的工作方式与普通的Map实现相当类似。

Let’s start by creating a Multimap instance and adding some entries:

让我们先创建一个Multimap实例并添加一些条目。

Multimap<Integer, String> mmap = Multimaps
  .newMultimap(new HashMap<>(), Sets::newHashSet);
mmap.put(1, "one");
mmap.put(1, "1");

And now we can assert:

而现在我们可以断言。

assertThat(mmap)
  .hasSize(2)
  .containsKeys(1)
  .contains(entry(1, "one"))
  .contains(entry(1, "1"));

There are also two additional assertions available – with subtle difference between them:

还有两个额外的断言可用–它们之间有微妙的区别。

  • containsAllEntriesOf and
  • hasSameEntriesAs.

Let’s have a look at these two assertions; we’ll start by defining a few maps:

让我们来看看这两个断言;我们先来定义几个地图。

Multimap<Integer, String> mmap1 = ArrayListMultimap.create();
mmap1.put(1, "one");
mmap1.put(1, "1");
mmap1.put(2, "two");
mmap1.put(2, "2");

Multimap<Integer, String> mmap1_clone = Multimaps
  .newSetMultimap(new HashMap<>(), HashSet::new);
mmap1_clone.put(1, "one");
mmap1_clone.put(1, "1");
mmap1_clone.put(2, "two");
mmap1_clone.put(2, "2");

Multimap<Integer, String> mmap2 = Multimaps
  .newSetMultimap(new HashMap<>(), HashSet::new);
mmap2.put(1, "one");
mmap2.put(1, "1");

As you can see, mmap1 and mmap1_clone contain exactly the same entries, but are two different objects of two different Map types. The Map mmap2 contains a single entry that is shared among all maps. Now the following assertion is true:

正如你所看到的,mmap1mmap1_clone包含完全相同的条目,但却是两种不同的Map类型的两个对象。Map mmap2包含一个在所有地图中共享的条目。现在,下面的断言是真的。

assertThat(mmap1)
  .containsAllEntriesOf(mmap2)
  .containsAllEntriesOf(mmap1_clone)
  .hasSameEntriesAs(mmap1_clone);

3.3. Optional Assertions

3.3.可选的断言

Assertions for Guava’s Optional involve value presence checking and utilities for extracting the inner value.

Guava的Optional断言涉及值的存在检查和提取内部值的实用程序。

Let’s start by creating an Optional instance:

让我们从创建一个Optional实例开始。

Optional<String> something = Optional.of("something");

And now we can check the value’s presence and assert the Optional‘s content:

现在我们可以检查该值的存在,并断言Optional的内容。

assertThat(something)
  .isPresent()
  .extractingValue()
  .isEqualTo("something");

3.4. Range Assertions

3.4.范围断言

Assertions for Guava’s Range class involves checking Range‘s lower and upper bounds or whether a certain value is within a given range.

Guava的Range类的断言涉及到检查Range的下限和上限或者某个值是否在给定的范围内。

Let’s define a simple range of characters by doing the following:

让我们通过以下方式定义一个简单的字符范围。

Range<String> range = Range.openClosed("a", "g");

and now we can test:

现在我们可以测试了。

assertThat(range)
  .hasOpenedLowerBound()
  .isNotEmpty()
  .hasClosedUpperBound()
  .contains("b");

3.5. Table Assertions

3.5.断言

AssertJ’s table-specific assertions allow the checking of row and column count and the presence of a cells value.

AssertJ的表格特定断言允许检查行和列的数量以及单元格值的存在。

Let’s create a simple Table instance:

让我们创建一个简单的Table实例。

Table<Integer, String, String> table = HashBasedTable.create(2, 2);
table.put(1, "A", "PRESENT");
table.put(1, "B", "ABSENT");

and now we can perform the following check:

而现在我们可以进行以下检查。

assertThat(table)
  .hasRowCount(1)
  .containsValues("ABSENT")
  .containsCell(1, "B", "ABSENT");

4. Conclusion

4.结论

In this article from AssertJ series, we explored all Guava-related features.

在AssertJ系列的这篇文章中,我们探讨了所有与Guava相关的功能。

The implementation of all the examples and code snippets can be found in a GitHub project.

所有例子和代码片段的实现都可以在GitHub项目中找到。

Next »

AssertJ’s Java 8 Features

« Previous

Introduction to AssertJ