This guide is for the AssertJ core module. Quick start guides for other modules are here:
AssertJ artifacts are in the Maven central repository.
<dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <!-- use 2.9.1 for Java 7 projects --> <version>3.11.1</version> <scope>test</scope> </dependency>
For Gradle users (using the Maven Central Repository)
Or version 2.9.1 for Java 7 projects
If you use another dependency management tool, please check this page to find the relevant assertj dependency declaration.
One static import to rule them all ...
import static org.assertj.core.api.Assertions.*;
... or many if you prefer
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
On Android (API Level < 26) use this static import :
import static org.assertj.core.api.Java6Assertions.*;
You can even configure your IDE, so that when you type as and trigger code completion, it will suggest assertThat, see this tip.
Type assertThat followed by the object under test and a dot ... and any Java IDE code completion will show you all available assertions.
assertThat(objectUnderTest). // code completion -> assertions specific to objectUnderTest
Code completion example for String assertions :
See some of AssertJ noteworthy features to get the maximum out of it !
You can learn more by looking at the AssertJ core javadoc.
Another way to extend your knowledge is the assertj-examples tests project. It covers what is possible with AssertJ Assertions with running code. You can clone the repository and run its tests ! (You need to run `mvn install` first, as the project also shows how to generate custom assertions.)