Skip to content

One minute starting guide

joel-costigliola edited this page May 16, 2012 · 37 revisions

(to main page)

1 - Download Fest assertions

Get the Fest Assertions 2.0M3 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.0M3</version>
</dependency>

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

2 - Add Assertions.* static import

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

This static import will give you access to asserThat, fail, delta and entry methods (all with clean javadoc).

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