Core assertions guide
This section describes the assertions provided by AssertJ Core and other useful features to get the best of it.
AssertJ Core Javadoc explains each assertion, most of them with code examples so be sure to check it if you want to know what a specific assertion does.
A simple example
Let’s start with a simple example showing a few important things.
import static org.assertj.core.api.Assertions.assertThat; (1)
import org.junit.jupiter.api.Test;
public class SimpleAssertionsExample {
@Test
void a_few_simple_assertions() {
assertThat("The Lord of the Rings").isNotNull() (2) (3)
.startsWith("The") (4)
.contains("Lord") (4)
.endsWith("Rings"); (4)
}
}
| 1 | Statically import org.assertj.core.api.Assertions.assertThat |
| 2 | Pass the object under test as the sole assertThat() parameter |
| 3 | Use code completion to discover and call assertions |
| 4 | Chain as many assertions as you need |
Except for isNotNull which is a base assertion, the other assertions are String specific as our object under test is a String.
Javadoc
www.javadoc.io/doc/org.assertj/assertj-core/3.27.8-SNAPSHOT is the latest version of AssertJ Core Javadoc, each assertion is explained, most of them with code examples so be sure to check it if you want to know what a specific assertion does.