One minute starting guide

joel-costigliola edited this page Feb 27, 2013 · 37 revisions

(to main page)

1 - Get Fest assertions

For Maven user, Fest artifacts are in Maven central repository


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

2 - Add Assertions.* static import

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

or the complete list

import static org.fest.assertions.api.Assertions.assertThat; // main one
import static org.fest.assertions.api.Assertions.atIndex; // for List assertion
import static org.fest.assertions.api.Assertions.entry;  // for Map assertion
import static org.fest.assertions.api.Assertions.extractProperty; // for Iterable/Array assertion
import static; // use when making exception tests
import static org.fest.assertions.api.Assertions.failBecauseExceptionWasNotThrown; // idem
import static org.fest.assertions.api.Assertions.filter; // for Iterable/Array assertion
import static org.fest.assertions.api.Assertions.offset; // for floating number assertion
import static org.fest.assertions.api.Assertions.anyOf; // use with Condition

You can even configure your IDE, so that when you type asse and trigger code completion, it will suggest assertThat, see this tip.

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.api.Assertions.*;

// common assertions

// String specific assertions

// collection specific assertions
                               .contains(frodo, sam)
// throwable specific assertions
try {
  fellowshipOfTheRing.get(9); // argggl !
  // if IndexOutOfBoundsException was not thrown, test would fail with message : 
  // "Expected IndexOutOfBoundsException to be thrown"
} catch (Exception e) {
               .hasMessage("Index: 9, Size: 9")

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

4 - Want to see more ?

See our Tips and tricks page for best practices !

Have a 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