β’ Custom Assertions with AssertJ
1. Overview
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.
2. Maven Dependencies
In order to use AssertJ with Guava, you need to add the following dependency to your 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. Guava Assertions in Action
AssertJ has custom assertions for Guava types: ByteSource, Multimap, Optional, Range, RangeMap and Table.
3.1. ByteSource Assertions
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 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
Multimaps are maps that can associate more than one value with a given key. The Multimap assertions work pretty similarly to normal Map implementations.
Letβs start by creating a Multimap instance and adding some entries:
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:
assertThat(mmap1)
.containsAllEntriesOf(mmap2)
.containsAllEntriesOf(mmap1_clone)
.hasSameEntriesAs(mmap1_clone);
3.3. Optional Assertions
Assertions for Guavaβs Optional involve value presence checking and utilities for extracting the inner value.
Letβs start by creating an Optional instance:
Optional<String> something = Optional.of("something");
And now we can check the valueβs presence and assert the Optionalβs content:
assertThat(something)
.isPresent()
.extractingValue()
.isEqualTo("something");
3.4. Range Assertions
Assertions for Guavaβs Range class involves checking Rangeβs lower and upper bounds or whether a certain value is within a given 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
AssertJβs table-specific assertions allow the checking of row and column count and the presence of a cells value.
Letβs create a simple Table instance:
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
In this article from AssertJ series, we explored all Guava-related features.
