Skip to content

One minute starting guide

joel-costigliola edited this page Apr 15, 2012 · 37 revisions

(to main page)

1 - Download Fest assertions

Get the Fest Assertions 2.0M1 zip archive, then extract the jars to your classpath.

For Maven user, Fest artifacts are in Maven central repository

<dependency>
   <groupId>org.easytesting</groupId>
   <artifactId>fest-assert-core</artifactId>
   <version>2.0M1</version>
</dependency>

Other dependencies coordinates (Ivy, Grape, ...) are described here.

2 - Add Assertions.assertThat static import

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

3 - Type assertThat followed by the actual value and a dot ...

... and any Java IDE code completion will show you all the assertions available.

That's all !

Here are some examples:

import static org.fest.assertions.Assertions.assertThat;
import static org.fest.assertions.MapAssert.entry;

// 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(Ring.oneRing, frodo), entry(Ring.nenya, galadriel))
                       .excludes(entry(Ring.oneRing, aragorn));

4 - Want to see more examples ?

Have a direct look at fest-examples project sources, it covers what is possible with Fest Assertions.
Or even better fork it and run it as JUnit tests ! Repo is here : fest-examples