or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced.mdbasic-assertions.mdcollections.mdconditions.mddates-times.mdexceptions.mdindex.mdsoft-assertions.mdstrings.md
tile.json

tessl/maven-org-assertj--assertj-core

Fluent assertion library providing rich assertions for Java tests with expressive failure messages

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.assertj/assertj-core@3.27.x

To install, run

npx @tessl/cli install tessl/maven-org-assertj--assertj-core@3.27.0

index.mddocs/

AssertJ Core

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

Package Information

  • Package Name: org.assertj:assertj-core
  • Package Type: maven
  • Language: Java
  • Installation:
    • Maven: <dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>3.27.3</version><scope>test</scope></dependency>
    • Gradle: testImplementation 'org.assertj:assertj-core:3.27.3'

Core Imports

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;

Alternative Entry Points

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
    }
}

Basic Usage

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 failures

Architecture

AssertJ follows a fluent API pattern where:

  1. Entry Points: Static methods in Assertions class create Assert instances
  2. Assert Classes: Type-specific classes provide focused assertion methods
  3. Chaining: Most assertions return the Assert instance for method chaining
  4. Failure Messages: Descriptive error messages show expected vs actual values

Capabilities

Basic Assertions

Core 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()

Basic Assertions

Collections

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)

Collections

Strings

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

Strings

Dates and Times

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)

Dates and Times

Exceptions

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)

Exceptions

Soft Assertions

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

Soft Assertions

Conditions

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)

Conditions

Advanced Features

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>

Advanced Features

Types

// 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)
}