AssertJ Core new website!

AssertJ Core site has moved to https://assertj.github.io/doc/

One minute starting guide

This guide is for the AssertJ core module. Quick start guides for other modules are here:

1 - Get AssertJ Core assertions

Maven

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>

Gradle

For Gradle users (using the Maven Central Repository)

testCompile("org.assertj:assertj-core:3.11.1")

Or version 2.9.1 for Java 7 projects

testCompile("org.assertj:assertj-core:2.9.1")

If you use another dependency management tool, please check this page to find the relevant assertj dependency declaration.

2 - Add AssertJ methods static import

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.

3 - Use code completion

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 :

Screenshot of IDE completion

4 - Want to see more ?

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


AltStyle によって変換されたページ (->オリジナル) /