Fluent assertion library providing rich assertions for Java tests with expressive failure messages
npx @tessl/cli install tessl/maven-org-assertj--assertj-core@3.27.0AssertJ Core is a rich assertion library for Java that provides fluent assertions with descriptive error messages. It offers 200+ static assertion methods and 80+ Assert classes for comprehensive testing of all Java types, from primitives to complex objects, collections, dates, exceptions, and more.
<dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>3.27.3</version><scope>test</scope></dependency>testImplementation 'org.assertj:assertj-core:3.27.3'import static org.assertj.core.api.Assertions.*;For specific use cases:
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;AssertJ provides specialized entry points for different environments and testing frameworks:
// Java 6 compatibility entry point
import static org.assertj.core.api.Java6Assertions.*;
// JUnit 5 specific entry point
import static org.assertj.core.api.JUnitJupiterAssertions.*;
// JUnit soft assertions entry point
import static org.assertj.core.api.JUnitSoftAssertions.*;Usage examples:
// Java 6 Assertions - compatible with older Java versions
import static org.assertj.core.api.Java6Assertions.*;
assertThat("Hello").isEqualTo("Hello");
assertThat(Arrays.asList(1, 2, 3)).hasSize(3);
// JUnit Jupiter Assertions - optimized for JUnit 5
import static org.assertj.core.api.JUnitJupiterAssertions.*;
@Test
void testWithJUnitJupiterAssertions() {
assertThat("JUnit 5").startsWith("JUnit");
assertThatThrownBy(() -> { throw new RuntimeException(); })
.isInstanceOf(RuntimeException.class);
}
// JUnit Soft Assertions - JUnit-integrated soft assertions
import static org.assertj.core.api.JUnitSoftAssertions.*;
@ExtendWith(SoftAssertionsExtension.class)
class SoftAssertionsTest {
@Test
void testWithJUnitSoftAssertions(SoftAssertions softly) {
softly.assertThat("Hello").startsWith("Hi");
softly.assertThat(42).isNegative();
// Failures collected and reported automatically by JUnit extension
}
}import static org.assertj.core.api.Assertions.*;
// Basic assertions
assertThat("Hello World").startsWith("Hello").endsWith("World");
assertThat(42).isGreaterThan(40).isLessThan(50);
assertThat(Arrays.asList(1, 2, 3)).hasSize(3).contains(2);
// Exception testing
assertThatThrownBy(() -> {
throw new IllegalArgumentException("Invalid input");
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid input");
// Soft assertions (collect all failures)
SoftAssertions softly = new SoftAssertions();
softly.assertThat("Hello").startsWith("Hi");
softly.assertThat(42).isNegative();
softly.assertAll(); // throws AssertionError with all failuresAssertJ follows a fluent API pattern where:
Assertions class create Assert instancesCore assertion patterns for primitive types, objects, and common operations.
// Primitive assertions
assertThat(boolean actual) → BooleanAssert
assertThat(int actual) → IntegerAssert
assertThat(String actual) → StringAssert
assertThat(Object actual) → ObjectAssert
// Common assertion methods
BooleanAssert isTrue()
BooleanAssert isFalse()
IntegerAssert isPositive()
IntegerAssert isNegative()
IntegerAssert isEven()
IntegerAssert isOdd()Comprehensive assertions for arrays, lists, maps, and iterables with size, content, and order verification.
// Collection assertions
assertThat(List<T> actual) → ListAssert<T>
assertThat(T[] actual) → ObjectArrayAssert<T>
assertThat(Map<K,V> actual) → MapAssert<K,V>
assertThat(Iterable<T> actual) → IterableAssert<T>
// Collection methods
ListAssert<T> hasSize(int expected)
ListAssert<T> contains(T... values)
ListAssert<T> containsExactly(T... values)
MapAssert<K,V> containsKey(K key)
MapAssert<K,V> containsEntry(K key, V value)String and CharSequence assertions for content, format, and pattern matching.
// String assertions
assertThat(String actual) → StringAssert
assertThat(CharSequence actual) → CharSequenceAssert
// String methods
StringAssert startsWith(String prefix)
StringAssert endsWith(String suffix)
StringAssert contains(String substring)
StringAssert matches(String regex)
StringAssert hasLength(int length)
StringAssert isEmpty()
StringAssert isBlank()Date and time assertions supporting both legacy Date and modern java.time API.
// Date/Time assertions
assertThat(LocalDate actual) → LocalDateAssert
assertThat(LocalDateTime actual) → LocalDateTimeAssert
assertThat(Instant actual) → InstantAssert
assertThat(Duration actual) → DurationAssert
// Date/Time methods
LocalDateAssert isBefore(LocalDate date)
LocalDateTimeAssert isAfter(LocalDateTime dateTime)
InstantAssert isCloseTo(Instant instant, TemporalUnitWithinOffset offset)
DurationAssert isEqualTo(Duration expected)Exception testing with ThrowableAssert for verifying exception types, messages, and causes.
// Exception assertions
assertThatThrownBy(ThrowingCallable callable) → ThrowableAssert
assertThatExceptionOfType(Class<? extends Throwable> type) → ThrowableTypeAssert
assertThatCode(ThrowingCallable callable) → NotThrownAssert
// Exception methods
ThrowableAssert hasMessage(String message)
ThrowableAssert isInstanceOf(Class<?> type)
ThrowableAssert hasCause(Throwable cause)
ThrowableAssert hasRootCause(Throwable rootCause)Collect multiple assertion failures and report them together instead of failing on first error.
// Soft assertions
SoftAssertions softly = new SoftAssertions()
AutoCloseableSoftAssertions softly = new AutoCloseableSoftAssertions()
// JUnit integration
@RegisterExtension
JUnitSoftAssertions softly = new JUnitSoftAssertions()
// Soft assertion methods
void assertAll()
List<AssertionError> assertionErrorsCollected()Custom conditions and condition combinators for reusable and composable assertions.
// Condition creation
Condition<T> allOf(Condition<? super T>... conditions)
Condition<T> anyOf(Condition<? super T>... conditions)
Condition<T> not(Condition<? super T> condition)
// Using conditions
ObjectAssert<T> is(Condition<? super T> condition)
ObjectAssert<T> has(Condition<? super T> condition)
ObjectAssert<T> satisfies(Condition<? super T> condition)Advanced features including filters, groups, atomic types, concurrent types, and utilities.
// Filtering and extraction
Filters<E> filter(E[] array)
ObjectArrayAssert<T> extracting(String... properties)
// Atomic types
assertThat(AtomicInteger actual) → AtomicIntegerAssert
assertThat(AtomicReference<T> actual) → AtomicReferenceAssert<T>
// Concurrent types
assertThat(CompletableFuture<T> actual) → CompletableFutureAssert<T>
assertThat(Future<T> actual) → FutureAssert<T>// Core interfaces
interface ThrowingCallable {
void call() throws Throwable;
}
interface Condition<T> {
boolean matches(T value);
String description();
}
// Common classes
class Offset<T extends Number> {
static Offset<Double> offset(Double value)
static Offset<Float> offset(Float value)
}
class Index {
static Index atIndex(int index)
}
class MapEntry<K, V> {
static <K, V> MapEntry<K, V> entry(K key, V value)
}