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.