Assertj-core

AssertJ is a library of assertions similar to fest-assert but providing a richer set of assertions.


Project maintained by joel-costigliola Hosted on GitHub Pages — Theme by mattgraham

AssertJ - a rich assertions library for java

AssertJ provides a rich and intuitive set of strongly typed assertions to use for unit testing (either with JUnit or TestNG).

You can ask questions and make suggestions on AssertJ google group.
To directly jump to a more complete documentation please go to AssertJ wiki.

AssertJ's goals

AssertJ's ambition is to provide a rich and intuitive set of strongly typed assertions to use for unit testing.
The idea is that, when writing unit tests, we should have at our disposal assertions specific to the type of the objects we are checking : you are checking a String ? use String specific assertions !

AssertJ is composed of several modules :

Assertion missing ? Please fill a ticket !

AssertJ's assertions are super 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 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.
A lot of efforts have to provide intuitive error messages showing as clearly as possible what the problem is.

Note that AssertJ requires at least Java 6.

For more details check AssertJ wiki.

Latest News

2013-03-28 : AssertJ quickie presentation at Devoxx France !

2013-03-26 - AssertJ train releases :

See what's new in assertj-core compared to Fest.

Quickstart

It is easy to start using AssertJ, it should take you less than a minute !

1 - Get AssertJ core

AssertJ core is available in Maven central repository.

<dependency>
   <groupId>org.assertj</groupId>
   <artifactId>assertj-core</artifactId>
   <version>1.0.0</version>
   <scope>test</scope>
</dependency>

2 - Add Assertions.* static import

import static org.assertj.core.api.Assertions.*;

or the complete list

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

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

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 !

Some assertions examples

import static org.assertj.core.api.Assertions.*;

// some of the assertions available for all types
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)
                               .doesNotContain(sauron);
// Exception/Throwable specific assertions
try {
  fellowshipOfTheRing.get(9); 
  // argggl ! fellowshipOfTheRing size is 9 and get(9) asks for the 10th element !
} catch (Exception e) {
  assertThat(e).isInstanceOf(IndexOutOfBoundsException.class)
               .hasMessage("Index: 9, Size: 9")
               .hasNoCause();
}

// Map specific assertions, ringBearers is a Map of Ring -> TolkienCharacter
assertThat(ringBearers).hasSize(4)
                       .includes(entry(Ring.oneRing, frodo), entry(Ring.nenya, galadriel))
                       .excludes(entry(Ring.oneRing, aragorn));

Assertions for your own custom types

Having assertions for common types like List is great but it would so nice to have some for your own types !

Well this is possible with AssertJ because it is easily extensible so it's simple to write assertions for your custom types.
Moreover, to ease your work, we provide assertions generator that can take a bunch of custom types and create for you specific assertions, we provide :

Replacing JUnit assertions by AssertJ Assertions

To help you replace JUnit assertions by AssertJ ones, follow the steps described here, they are based on regex search and replace.

Why having forked Fest Assert ?

AssertJ is a fork of FEST Assert a great project I have contributed to during 3 years, so why having forked it ?

Well there are two main reasons :

On the contrary AssertJ goal is to provide a rich set of assertions and any resonable requests to enrich AssertJ assertions is welcome as we know it will be useful to someone. Said otherwise, AssertJ is community driven, we listen to our users because AssertJ is built for them.

If you feel that some assertion is missing, please fill a ticket or even better make a contribution !

Joel Costigliola (AssertJ creator)

Migrating from Fest assertions

As AssertJ starts where Fest 2.0M10 has left, migrating from Fest to AssertJ Assertions is easy, you only have to change your static import, just replace :

import static org.fest.assertions.api.Assertions

by :

import static org.assertj.core.api.Assertions

This should be all, if not please fill a ticket so that I can update this section.

If you are using Fest Assert 1.x, please read this migration guide.

Documentation & Mailing list

You can ask questions and make suggestions on AssertJ google group.
To directly jump to a more complete documentation please go to AssertJ wiki.

Want to contribute ?

You are very welcome to contribute any missing useful assertions, please check the contributor guidelines.