or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-assertions.mdcollection-assertions.mdcore-assertions.mdcustom-assertions.mdexception-assertions.mdindex.mdjava8-assertions.mdmap-assertions.mdnumeric-assertions.mdstring-assertions.mdtesting-utilities.md
tile.json

tessl/maven-com-google-truth--truth

Fluent assertion framework for Java that provides clear, readable test assertions and informative failure messages

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.google.truth/truth@1.4.x

To install, run

npx @tessl/cli install tessl/maven-com-google-truth--truth@1.4.0

index.mddocs/

Google Truth

Google Truth is a fluent assertion framework for Java that provides clear, readable test assertions and informative failure messages. Similar to AssertJ, it natively supports many JDK and Guava types, and it is extensible to others. Truth is owned and maintained by the Guava team and is used in the majority of the tests in Google's own codebase.

Package Information

  • Package Name: com.google.truth:truth
  • Package Type: maven
  • Language: Java
  • Installation: Add to Maven pom.xml:
    <dependency>
      <groupId>com.google.truth</groupId>
      <artifactId>truth</artifactId>
      <version>1.4.4</version>
      <scope>test</scope>
    </dependency>

Core Imports

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

For custom subjects:

import static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.Subject;

Basic Usage

import static com.google.common.truth.Truth.assertThat;

// Basic assertions
assertThat("actual").isEqualTo("expected");
assertThat(42).isGreaterThan(20);
assertThat(list).contains("item");

// With custom failure messages
assertWithMessage("User should be active").that(user.isActive()).isTrue();

// Collection assertions
assertThat(Arrays.asList(1, 2, 3))
    .containsExactly(1, 2, 3)
    .inOrder();

Architecture

Truth is built around several key components:

  • Entry Points: Truth class provides static factory methods for creating assertion chains
  • Subject Hierarchy: 40+ specialized subject classes for different types (String, Integer, Iterable, etc.)
  • Builder Pattern: StandardSubjectBuilder allows message customization and custom subject creation
  • Extension System: Subject.Factory and CustomSubjectBuilder enable custom assertion types
  • Failure Strategy: Pluggable failure handling with rich error messages and facts

Capabilities

Core Assertions

Foundation assertion methods available on all subjects for equality, nullity, type checking, and basic comparisons.

// Truth entry points
public static StandardSubjectBuilder assert_();
public static StandardSubjectBuilder assertWithMessage(String message);
public static StandardSubjectBuilder assertWithMessage(String format, Object... args);

// Extension entry points
public static <S extends Subject, T> SimpleSubjectBuilder<S, T> assertAbout(Subject.Factory<S, T> factory);
public static <T extends CustomSubjectBuilder> T assertAbout(CustomSubjectBuilder.Factory<T> factory);

// Core assertThat methods
public static Subject assertThat(Object actual);
public static <T extends Comparable<?>> ComparableSubject<T> assertThat(T actual);
public static BooleanSubject assertThat(Boolean actual);
public static ClassSubject assertThat(Class<?> actual);

// Basic Subject methods
public void isNull();
public void isNotNull();
public void isEqualTo(Object expected);
public void isNotEqualTo(Object expected);
public void isSameInstanceAs(Object expected);
public void isNotSameInstanceAs(Object expected);
public void isInstanceOf(Class<?> type);
public void isNotInstanceOf(Class<?> type);

Core Assertions

String Assertions

Comprehensive string-specific assertion methods for length, content, pattern matching, and case-insensitive comparisons.

public static StringSubject assertThat(String actual);

// Key StringSubject methods
public void isEmpty();
public void isNotEmpty();
public void hasLength(int expectedLength);
public void contains(CharSequence expectedSubstring);
public void startsWith(String prefix);
public void endsWith(String suffix);
public void matches(String regex);
public void containsMatch(String regex);

String Assertions

Numeric Assertions

Type-safe numeric assertions with comparison operations and tolerance-based floating-point comparisons.

public static IntegerSubject assertThat(Integer actual);
public static LongSubject assertThat(Long actual);
public static DoubleSubject assertThat(Double actual);
public static FloatSubject assertThat(Float actual);
public static BigDecimalSubject assertThat(BigDecimal actual);

// ComparableSubject methods (inherited by numeric types)
public void isAtLeast(T other);
public void isAtMost(T other);
public void isGreaterThan(T other);
public void isLessThan(T other);

// Tolerance-based comparisons for floating-point types
public TolerantDoubleComparison isWithin(double tolerance);
public TolerantFloatComparison isWithin(float tolerance);

Numeric Assertions

Collection Assertions

Rich assertion methods for iterables, lists, sets, and other collections including size, containment, ordering, and exact matching.

public static IterableSubject assertThat(Iterable<?> actual);

// Core collection methods
public void isEmpty();
public void isNotEmpty();
public void hasSize(int expectedSize);
public void contains(Object element);
public void containsExactly(Object... elements);
public void containsExactlyElementsIn(Iterable<?> expected);
public void containsAtLeast(Object... elements);
public void containsAnyOf(Object... elements);
public void containsNoneOf(Object... elements);

// Ordering assertions
public void isInOrder();
public void isInStrictOrder();
public Ordered containsExactly(Object... elements);

Collection Assertions

Map Assertions

Specialized assertions for maps including key-value pair validation, entry containment, and size verification.

public static MapSubject assertThat(Map<?, ?> actual);

// Map-specific methods
public void containsKey(Object key);
public void doesNotContainKey(Object key);
public void containsEntry(Object key, Object value);
public void doesNotContainEntry(Object key, Object value);
public void containsExactly();
public void containsExactly(Object k1, Object v1, Object k2, Object v2, ...);
public void containsExactlyEntriesIn(Map<?, ?> expectedMap);

Map Assertions

Guava Collection Assertions

Specialized assertions for Guava collection types including Multimap, Multiset, Table, and Optional.

public static MultimapSubject assertThat(Multimap<?, ?> actual);
public static MultisetSubject assertThat(Multiset<?> actual);
public static TableSubject assertThat(Table<?, ?, ?> actual);
public static GuavaOptionalSubject assertThat(com.google.common.base.Optional<?> actual);

// Guava collection-specific methods available on respective subjects
public void containsKey(Object key);
public void containsEntry(Object key, Object value);
public void hasSize(int expectedSize);

Path Assertions

File system path assertions for Java NIO Path objects.

public static PathSubject assertThat(Path actual);

Array Assertions

Comprehensive array support for both object arrays and all primitive array types with collection-like assertion methods.

public static <T> ObjectArraySubject<T> assertThat(T[] actual);
public static PrimitiveBooleanArraySubject assertThat(boolean[] actual);
public static PrimitiveIntArraySubject assertThat(int[] actual);
public static PrimitiveLongArraySubject assertThat(long[] actual);
public static PrimitiveDoubleArraySubject assertThat(double[] actual);
public static PrimitiveFloatArraySubject assertThat(float[] actual);
public static PrimitiveByteArraySubject assertThat(byte[] actual);
public static PrimitiveCharArraySubject assertThat(char[] actual);
public static PrimitiveShortArraySubject assertThat(short[] actual);

Array Assertions

Exception Assertions

Specialized assertions for throwables and exceptions including message and cause chain validation.

public static ThrowableSubject assertThat(Throwable actual);

// ThrowableSubject methods
public StringSubject hasMessageThat();
public ThrowableSubject hasCauseThat();

Exception Assertions

Java 8+ Type Assertions

Modern Java type support for Optional, Stream, and primitive optional types with appropriate assertion methods.

public static OptionalSubject assertThat(Optional<?> actual);
public static OptionalIntSubject assertThat(OptionalInt actual);
public static OptionalLongSubject assertThat(OptionalLong actual);
public static OptionalDoubleSubject assertThat(OptionalDouble actual);
public static StreamSubject assertThat(Stream<?> actual);
public static IntStreamSubject assertThat(IntStream actual);
public static LongStreamSubject assertThat(LongStream actual);

// Optional assertion methods
public void isPresent();
public void isEmpty();
public void hasValue(Object expected);

Java 8+ Assertions

Custom Assertions and Extensions

Extension mechanisms for creating custom subject types, correspondence-based comparisons, and advanced assertion patterns.

public static <S extends Subject, T> SimpleSubjectBuilder<S, T> assertAbout(Subject.Factory<S, T> factory);
public static <T extends CustomSubjectBuilder> T assertAbout(CustomSubjectBuilder.Factory<T> factory);

// Key extension interfaces
public interface Subject.Factory<SubjectT extends Subject, ActualT> {
    SubjectT createSubject(FailureMetadata metadata, ActualT actual);
}

// Correspondence for custom comparisons
public abstract class Correspondence<A, E> {
    public abstract boolean compare(A actual, E expected);
    public static <A, E> Correspondence<A, E> from(BinaryPredicate<A, E> predicate, String description);
}

Custom Assertions

Advanced Testing Utilities

Specialized utilities for testing assertion failures, soft assertions with batched failure reporting, and integration with testing frameworks.

// Expect - TestRule for soft assertions
public static Expect create();
public <T> Subject that(T actual);  // Collects failures instead of throwing

// ExpectFailure - Testing assertion failures
public static AssertionError expectFailure(SimpleSubjectBuilderCallback<?> assertionCallback);
public static AssertionError expectFailureAbout(Subject.Factory<S, A> factory, SubjectBuilderCallback<S> assertionCallback);

Testing Utilities

Types

// Core builder and subject types
public class StandardSubjectBuilder {
    public StandardSubjectBuilder withMessage(String message);
    public StandardSubjectBuilder withMessage(String format, Object... args);
    public <S extends Subject, A> SimpleSubjectBuilder<S, A> about(Subject.Factory<S, A> factory);
}

public class Subject {
    protected Subject(FailureMetadata metadata, Object actual);
    public interface Factory<SubjectT extends Subject, ActualT> {
        SubjectT createSubject(FailureMetadata metadata, ActualT actual);
    }
}

// Correspondence for custom comparisons
public abstract class Correspondence<A, E> {
    public abstract boolean compare(A actual, E expected);
    public String formatDiff(A actual, E expected);
}

// Fact for structured failure information
public final class Fact {
    public static Fact fact(String key, Object value);
    public static Fact simpleFact(String key);
}

// Ordered interface for ordering constraints
public interface Ordered {
    void inOrder();
}

// Failure handling
@FunctionalInterface
public interface FailureStrategy {
    void fail(AssertionError failure);
}