Using fest assertions - alexruiz/fest-assert-2.x GitHub Wiki

(to main page)

FEST's assertions are incredibly easy to write: just type assertThat followed the actual value and a dot, and any Java IDE will show you all the assertions available for the type of the given object to verify. No more confusion about the order of the "expected" and "actual" values.
Our assertions are very readable as well: they read very close to plain English, making it easier for non-technical people to read test code.

Here are some examples:

import static org.fest.assertions.api.Assertions.*;

// common assertions
assertThat(yoda).isInstanceOf(Jedi.class);
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron);
assertThat(frodo).isIn(fellowshipOfTheRing);
assertThat(sauron).isNotIn(fellowshipOfTheRing);

// String specific assertions
assertThat(frodo.getName()).startsWith("Fro").endsWith("do")
                           .isEqualToIgnoringCase("frodo");

// collection specific assertions
assertThat(fellowshipOfTheRing).hasSize(9)
                               .contains(frodo, sam)
                               .excludes(sauron);
// throwable specific assertions
try {
  fellowshipOfTheRing.get(9); // argggl !
} catch (Exception e) {
  assertThat(e).isInstanceOf(IndexOutOfBoundsException.class)
               .hasMessage("Index: 9, Size: 9")
               .hasNoCause();
}

// map specific assertions (One ring and elves ring bearers initialized before)
assertThat(ringBearers).hasSize(4)
                       .includes(entry(oneRing, frodo), entry(nenya, galadriel))
                       .excludes(entry(oneRing, aragorn));

Fest provides assertions for the following data types:

  • Object
  • String
  • Date
  • Primitives (boolean, int, char, etc.)
  • BigDecimal
  • Iterable
  • Arrays of Object
  • Arrays of primitives
  • Map
  • Throwable
  • File and InputStream
  • BufferedImage

See our Tips and tricks page for best practices !