AssertJ

Fluent assertions for java

// entry point for all assertThat methods and utility methods (e.g. entry)
import static org.assertj.core.api.Assertions.*;

// basic assertions
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron);

// chaining string specific assertions
assertThat(frodo.getName()).startsWith("Fro")
                           .endsWith("do")
                           .isEqualToIgnoringCase("frodo");

// collection specific assertions (there are plenty more)
// in the examples below fellowshipOfTheRing is a List<TolkienCharacter>
assertThat(fellowshipOfTheRing).hasSize(9)
                               .contains(frodo, sam)
                               .doesNotContain(sauron);

// as() is used to describe the test and will be shown before the error message
assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33);

// Java 8 exception assertion, standard style ...
assertThatThrownBy(() -> { throw new Exception("boom!"); }).hasMessage("boom!");
// ... or BDD style
Throwable thrown = catchThrowable(() -> { throw new Exception("boom!"); });
assertThat(thrown).hasMessageContaining("boom");

// using the 'extracting' feature to check fellowshipOfTheRing character's names (Java 7)
assertThat(fellowshipOfTheRing).extracting("name")
                               .contains("Boromir", "Gandalf", "Frodo", "Legolas")
// same thing using a Java 8 method reference
assertThat(fellowshipOfTheRing).extracting(TolkienCharacter::getName)
                               .doesNotContain("Sauron", "Elrond");

// extracting multiple values at once grouped in tuples (Java 7)
assertThat(fellowshipOfTheRing).extracting("name", "age", "race.name")
                               .contains(tuple("Boromir", 37, "Man"),
                                         tuple("Sam", 38, "Hobbit"),
                                         tuple("Legolas", 1000, "Elf"));

// filtering a collection before asserting in Java 7 ... 
assertThat(fellowshipOfTheRing).filteredOn("race", HOBBIT)
                               .containsOnly(sam, frodo, pippin, merry);
// ... or in Java 8
assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
                               .containsOnly(aragorn, frodo, legolas, boromir);

// combining filtering and extraction (yes we can)
assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o"))
                               .containsOnly(aragorn, frodo, legolas, boromir)
                               .extracting(character -> character.getRace().getName())
                               .contains("Hobbit", "Elf", "Man");

// and many more assertions : iterable, stream, array, map, dates (java 7 and java 8), path, file, numbers, predicate, optional ...

Want to see more features ? Jump to the Highlights section.

AssertJ is IDE friendly, use code completion to discover assertions :


Screenshot of IDE completion

Javadoc of assertion methods contains helpful examples, e.g. containsOnlyDigits


Rich and easy to use

AssertJ provides a rich set of assertions, truly helpful error messages, improves test code readability and is designed to be super easy to use within your favorite IDE.

Get started right away with the one minute starting guide, have a look at some of AssertJ great features or stay up to date with the latest releases.

If you are convinced by AssertJ, you can automatically convert your JUnit assertions to AssertJ.

Extensible

You can easily write assertions for your own classes, which will make your test assertions reflect the domain and is a way to use the ubiquitous language in your tests !

We provide an assertion generator to quickly create assertions for your domain model classes.

Community driven

AssertJ only exists to help the dev community. We listen to users ideas to provide the most useful assertions.

AssertJ is a fork of the great Fest Assert library which is not maintained anymore.

AssertJ will remain open and free forever.


AssertJ provides assertions for popular libraries

Guava

Provides assertions for Guava types like Multimap, Table, Optional or ByteSource.

Check AssertJ Guava assertions latest news and documentation.

Joda Time

Provides assertions for Joda Time types like DateTime and LocalDateTime. More will come later, contributions are very welcome !

Check AssertJ Joda Time assertions latest news and documentation.

Neo4J

Provides assertions for Neo4J types like Node, Path and Relationship.

Check AssertJ Neo4J assertions latest news and documentation.

Neo4J assertions have been developed by Florent Biville.