<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.20.2</version>
<scope>test</scope>
</dependency>
import org.assertj.core.api.Assertions;
Assertions.assertThat("Hello").isEqualTo("Hello");
Assertions.assertThat(42).isGreaterThan(10);
Assertions.assertThat(Arrays.asList(1, 2, 3)).contains(2);
Assertions.assertThat(null).isNull();
import org.assertj.core.api.Assertions.assertThat;
Person person = new Person("John", "Doe");
assertThat(person)
.hasFieldOrPropertyWithValue("firstName", "John")
.hasFieldOrProperty("lastName")
.doesNotHaveFieldOrProperty("age");
List<String> names = Arrays.asList("John", "Jane", "Jim");
assertThat(names)
.hasSize(3)
.contains("Jane")
.doesNotContain("Jerry");
import org.assertj.core.api.Assertions;
Assertions.assertThatThrownBy(() -> {
throw new IllegalArgumentException("Invalid argument");
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid argument");
import org.assertj.core.api.Assertions;
import org.assertj.core.configuration.Configuration;
Configuration configuration = Assertions.configuration();
configuration.failOnMissingFieldOrProperty(false);
configuration.arrayElementComparator(new CustomComparator());
configuration.apply();
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.Assertions;
public class CustomAssert extends AbstractAssert<CustomAssert, CustomObject> {
private CustomAssert(CustomObject actual) {
super(actual, CustomAssert.class);
}
public static CustomAssert assertThat(CustomObject actual) {
return new CustomAssert(actual);
}
public CustomAssert hasCustomProperty(String property) {
isNotNull();
if (!actual.hasCustomProperty(property)) {
failWithMessage("Expected custom property <%s> but was not found", property);
}
return this;
}
}
CustomObject customObject = new CustomObject();
Assertions.assertThat(customObject).hasCustomProperty("myProperty");