AssertJ Guava assertions

AssertJ assertions for Guava provides assertions for Guava types like Multimap, Multiset, Table, Optional, Range or ByteSource.

If you think some assertion is missing, please create an issue or even better contribute!

AssertJ Guava is hosted on github : https://github.com/joel-costigliola/assertj-guava.

To quickly start using Guava assertions, follow the steps below.

Maven

AssertJ artifacts are in the Maven central repository.

<dependency>
  <groupId>org.assertj</groupId>
  <artifactId>assertj-guava</artifactId>
  <!-- Use 2.x version if you rely on Java 7 / AssertJ Core 2.x -->
  <version>3.3.0</version>
  <scope>test</scope>
</dependency>

Gradle

For Gradle users (using the Maven Central Repository)

testCompile("org.assertj:assertj-guava:3.2.0")

Or version 2.0.1 for Java 7 projects

testCompile("org.assertj:assertj-guava:2.0.1")

If you use another dependency tool, please check this page to find the relevant assertj-guava dependency declaration for your tool.

... and use your preferred IDE code completion after assertThat.

Example :

import static org.assertj.guava.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.entry;

// Multimap assertions
Multimap<String, String> actual = ArrayListMultimap.create();
actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));

assertThat(actual).containsKeys("Lakers", "Spurs");
assertThat(actual).contains(entry("Lakers", "Kobe Bryant"), entry("Spurs", "Tim Duncan"));

// Multiset assertions
Multiset<String> actual = HashMultiset.create();
actual.add("shoes", 2);

assertThat(actual).contains(2, "shoes");
assertThat(actual).containsAtLeast(1, "shoes");
assertThat(actual).containsAtMost(3, "shoes");

// Range assertions
Range<Integer> range = Range.closed(10, 12);

assertThat(range).isNotEmpty()
                 .contains(10, 11, 12)
                 .hasClosedLowerBound()
                 .hasLowerEndpointEqualTo(10)
                 .hasUpperEndpointEqualTo(12);

// Table assertions
Table<Integer, String, String> bestMovies = HashBasedTable.create();

bestMovies.put(1970, "Palme d'Or", "M.A.S.H");
bestMovies.put(1994, "Palme d'Or", "Pulp Fiction");
bestMovies.put(2008, "Palme d'Or", "Entre les murs");
bestMovies.put(2000, "Best picture Oscar", "American Beauty");
bestMovies.put(2011, "Goldener Bär", "A Separation");

assertThat(bestMovies).hasRowCount(5).hasColumnCount(3).hasSize(5)
                      .containsValues("American Beauty", "A Separation", "Pulp Fiction")
                      .containsCell(1994, "Palme d'Or", "Pulp Fiction")
                      .containsColumns("Palme d'Or", "Best picture Oscar", "Goldener Bär")
                      .containsRows(1970, 1994, 2000, 2008, 2011);


// Optional assertions
Optional<String> optional = Optional.of("Test");
assertThat(optional).isPresent().contains("Test");

assertThat and entry are static imports from the Assertions class.

Note that you can find more working examples in the guava package of the assertj-examples project.

The release notes are published in the new website.

Release date : 2018-05-05

AssertJ Guava 3.2.1 requires Java 8 and AssertJ Core 3.11.1

  • Replace assertj-core version range dependency by a fixed version
  • Javadoc improvements

Release date : 2018-04-30

AssertJ Guava 3.2.0 requires Java 8 and AssertJ Core 3.x.

  • Use Guava 21+ which is java 8 compatible
  • Can be used with assertj-core 3.9.1+
  • Set Guava dependency as provided
  • Javadoc improvements

Release date : 2018-05-05

AssertJ Guava 2.0.1 requires Java 7 and AssertJ Core 2.x.

  • Cap Guava version to 20.0 as 21 requires java 8
  • Javadoc improvements

Release date : 2016-10-24

AssertJ Guava 3.1.0 requires Java 8 and AssertJ Core 3.x.

Allow to check the content of Multisets.

Example :

 Multiset<String> actual = HashMultiset.create();
 actual.add("shoes", 2);

 assertThat(actual).contains(2, "shoes");
 assertThat(actual).containsAtLeast(1, "shoes");
 assertThat(actual).containsAtMost(3, "shoes");

Release date : 2015-12-19

AssertJ Guava 3.0.0 requires Java 8 and AssertJ Core 3.x.

  • Breaking change : AssertJ Guava 3.0.0 relies on Java 8 and Assertj Core 3.x .
  • Improve OSGi manifest.

Release date : 2015-07-21

Thanks to William Delanoue and Pascal Schumacher for their contributions to this version.

Allow to compare the content of two multimaps who would not be equal because being of different types like SetMultimap and ListMultimap.

Example :

Multimap<String, String> actual = ArrayListMultimap.create();
listMultimap.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
listMultimap.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));

Multimap<String, String> setMultimap = TreeMultimap.create();
setMultimap.putAll("Spurs", newHashSet("Tony Parker", "Tim Duncan", "Manu Ginobili"));
setMultimap.putAll("Bulls", newHashSet("Michael Jordan", "Scottie Pippen", "Derrick Rose"));

// assertion will pass as listMultimap and setMultimap have the same content
assertThat(listMultimap).hasSameEntriesAs(setMultimap);

// this assertion FAILS even though both multimaps have the same content
assertThat(listMultimap).isEqualTo(setMultimap);

Verifies that the actual Multimap contains all entries of the given one (it might contain more entries).

Example :

Multimap<String, String> actual = ArrayListMultimap.create();
actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));
actual.putAll("Bulls", newArrayList("Michael Jordan", "Scottie Pippen", "Derrick Rose"));

Multimap<String, String> other = TreeMultimap.create();
other.putAll("Spurs", newHashSet("Tony Parker", "Tim Duncan"));
other.putAll("Bulls", newHashSet("Michael Jordan", "Scottie Pippen"));

// assertion will pass as other is a subset of actual.
assertThat(actual).containsAllEntriesOf(other);

// this assertion FAILS as other does not contain "Spurs -> "Manu Ginobili" and "Bulls" -> "Derrick Rose"
assertThat(other).containsAllEntriesOf(actual);

Allow to chain assertion on the content of the Optional using extractingValue().

Example :

Optional<Number> optional = Optional.of(12L);
assertThat(optional).extractingValue()
                    .isInstanceOf(Long.class)
                    .isEqualTo(12L);

There is a special support for String values with extractingCharSequence().

Example :

Optional<String> optional = Optional.of("Bill");
// extractingCharSequence allows to chain String specific assertions
assertThat(optional).extractingCharSequence()
                    .startsWith("Bi");

Release date : 2015-01-21

Bugfix release fixing hasUpperEndpointEqual assertion error message (thanks László Balázs-Csíki for reporting it).

Release date : 2014-10-27

Thanks to Simon Barbey and Jochen Kupperschmidt for their contributions to this version.

  • New Multimap assertion : isNotEmpty(). (Simon Barbey)
  • Javadoc code syntax highlighting.
  • Javadoc fix. (Jochen Kupperschmidt)

Release date : 2014-05-25

Thanks to David Harris and Marcin Kwaczyński for their contributions to this version.

  • Guava Range assertions (Marcin Kwaczyński)
  • New Table assertions (David Harris)
  • Javadoc improvements

Release date : 2013-12-27

Thanks to Andrew Gaul for his contributions to this version.

  • Guava ByteSource assertions (Andrew Gaul)
  • Javadoc improvements.

2013-03-26 : 1.0.0 release

First release after Fest fork.

Thanks to Jan Gorman for his contributions to this version.

  • Guava Table assertions (Jan Gorman)

Latest javadoc release : AssertJ Guava javadoc.

Migrating from Fest Guava to AssertJ Guava Assertions is easy, you only have to change your static import, just replace :

import static org.fest.assertions.api.GUAVA

with :

import static org.assertj.guava.api.Assertions

You will have to use two static imports : one for org.assertj.core.api.Assertions.assertThat to get core assertions and one org.assertj.guava.api.Assertions.assertThat for Guava assertions.

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
...
// assertThat comes from org.assertj.guava.api.Assertions.assertThat static import
Multimap<String, String> actual = ArrayListMultimap.create();
actual.putAll("Lakers", newArrayList("Kobe Bryant", "Magic Johnson", "Kareem Abdul Jabbar"));
actual.putAll("Spurs", newArrayList("Tony Parker", "Tim Duncan", "Manu Ginobili"));

assertThat(actual).hasSize(6);
assertThat(actual).containsKeys("Lakers", "Spurs");

// assertThat comes from org.assertj.core.api.Assertions.assertThat static import
assertThat("hello world").startsWith("hello");

If you have any questions, please use AssertJ google group.

AssertJ Guava is hosted on github : https://github.com/joel-costigliola/assertj-guava.

Please report bugs or missing features in AssertJ Guava issue tracker.

Thanks for your interest ! Please check our contributor's guidelines.

Special thanks to AssertJ Guava contributors:

  • Florent Biville
  • Max Daniline
  • William Delanoue
  • Frédéric Marcel Falliere
  • Andrew Gaul
  • Jan Gorman
  • Kornel Kiełczewski
  • David Harris
  • Marcin Kwaczyński
  • Pascal Schumacher