Skip to content

One minute starting guide

joel-costigliola edited this page Jul 9, 2012 · 37 revisions

(to main page)

1 - Download Fest assertions

Get the Fest Assertions 2.0M6 zip archive, then extract the jars to your classpath (it contains fest lib dependencies).

For Maven user, Fest artifacts are in Maven central repository

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

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 org.fest.assertions.api.Assertions.fail; // 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
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 !
  // if IndexOutOfBoundsException was not thrown, test would fail with message : 
  // "Expected IndexOutOfBoundsException to be thrown"
  failBecauseExceptionWasNotThrown(IndexOutOfBoundsException.class);
} 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 ?

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