Introduction to AssertJ [Snippets]

DZone 's Guide to

Introduction to AssertJ [Snippets]

Want a simple introduction to the ever-helpful AssertJ? See how to put this popular library to use in your projects to simplify your test code.

· Java Zone ·
Free Resource

AssertJ is a library used to provide assert statements for Java. It is a fork of the FEST-Assert library.

assertThat() is the main method to be used.

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

// common assertions

// String specific assertions

// collection specific assertions
                               .contains(frodo, sam)

// using extracting magical feature to check fellowshipOfTheRing characters name :)
assertThat(fellowshipOfTheRing).extracting("name").contains("Boromir", "Gandalf", "Frodo", "Legolas")
                                                  .doesNotContain("Sauron", "Elrond");

// map specific assertions, ringBearers initialized with the elves rings and the one ring bearers.
                       .contains(entry(oneRing, frodo), entry(nenya, galadriel))
                       .doesNotContainEntry(oneRing, aragorn);

AssertJ's Advantages

AssertJ contains a large number of statements and error messages and helps to simplify test code, thus, improving its readability.

Date today = new Date();
assertTrue((birthday.getTime() > today.getTime()));

It also provides clear messages about test errors:

List<String> list = new ArrayList<>();
java.lang.AssertionError at ...

        to contain:
        but could not find:

Project Creation


Assert artifacts are in a central Maven repository.

    <!-- use 2.9.0 for Java 7 projects -->


To use Grable (a central Maven repository is used):

testCompile 'org.assertj:assertj-core:3.9.0'

Or 2.9.0 version for projects with Java 7 used:

testCompile 'org.assertj:assertj-core:2.9.0'

To use AssertJ methods, static imports should be added:

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.tuple; // when extracting several properties at once
import static org.assertj.core.api.Assertions.fail; // use when writing 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

Or one static import:

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

The main methods to be used is assertThat():

assertThat(objectUnderTest). // code completion -> assertions specific to objectUnderTest

Example of String assertions:

assertj ,java ,java testing ,test automation

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}