Fluent assertion framework for Java that provides clear, readable test assertions and informative failure messages
—
Comprehensive array support for both object arrays and all primitive array types with collection-like assertion methods.
Assertions for arrays of any object type, providing collection-like functionality.
/**
* Creates an ObjectArraySubject for asserting about object arrays.
* @param actual the object array under test
*/
public static <T> ObjectArraySubject<T> assertThat(T[] actual);Methods for basic array validation including size, emptiness, and element access.
/**
* Fails if the array is not empty.
*/
public void isEmpty();
/**
* Fails if the array is empty.
*/
public void isNotEmpty();
/**
* Fails if the array does not have the given length.
* @param expectedLength the expected array length
*/
public void hasLength(int expectedLength);Usage Examples:
String[] fruits = {"apple", "banana", "cherry"};
assertThat(fruits).isNotEmpty();
assertThat(fruits).hasLength(3);
String[] emptyArray = {};
assertThat(emptyArray).isEmpty();
assertThat(emptyArray).hasLength(0);Methods for checking array element containment and membership.
/**
* Fails if the array does not contain the given element.
* @param element the element that should be present
*/
public void contains(Object element);
/**
* Fails if the array contains the given element.
* @param element the element that should not be present
*/
public void doesNotContain(Object element);
/**
* Fails if the array does not contain at least one of the given elements.
* @param first the first element to check for
* @param rest additional elements to check for
*/
public void containsAnyOf(Object first, Object... rest);
/**
* Fails if the array contains any of the given elements.
* @param first the first element to check against
* @param rest additional elements to check against
*/
public void containsNoneOf(Object first, Object... rest);Usage Examples:
String[] colors = {"red", "green", "blue"};
assertThat(colors).contains("red");
assertThat(colors).doesNotContain("yellow");
assertThat(colors).containsAnyOf("red", "purple", "orange");
assertThat(colors).containsNoneOf("yellow", "black", "white");Methods for asserting exact array contents with optional ordering constraints.
/**
* Fails if the array does not contain exactly the given elements, in any order.
* @param elements the expected elements
*/
public Ordered containsExactly(Object... elements);
/**
* Fails if the array does not contain exactly the elements in the given iterable, in any order.
* @param expected the iterable containing expected elements
*/
public Ordered containsExactlyElementsIn(Iterable<?> expected);
/**
* Fails if the array does not contain exactly the elements in the given array, in any order.
* @param expected the array containing expected elements
*/
public Ordered containsExactlyElementsIn(Object[] expected);Usage Examples:
String[] fruits = {"apple", "banana", "cherry"};
// Order doesn't matter
assertThat(fruits).containsExactly("cherry", "apple", "banana");
// With ordering constraint
assertThat(fruits).containsExactly("apple", "banana", "cherry").inOrder();
// From another collection
List<String> expected = Arrays.asList("apple", "banana", "cherry");
assertThat(fruits).containsExactlyElementsIn(expected).inOrder();Methods for asserting that arrays contain at least the specified elements.
/**
* Fails if the array does not contain at least the given elements.
* @param elements the elements that should all be present
*/
public void containsAtLeast(Object... elements);
/**
* Fails if the array does not contain at least the elements in the given iterable.
* @param expected the iterable containing elements that should all be present
*/
public void containsAtLeastElementsIn(Iterable<?> expected);
/**
* Fails if the array does not contain at least the elements in the given array.
* @param expected the array containing elements that should all be present
*/
public void containsAtLeastElementsIn(Object[] expected);Usage Examples:
String[] fruits = {"apple", "banana", "cherry", "date"};
assertThat(fruits).containsAtLeast("apple", "cherry");
List<String> required = Arrays.asList("banana", "date");
assertThat(fruits).containsAtLeastElementsIn(required);Methods for asserting array element ordering using natural or custom comparison.
/**
* Fails if the array is not in natural order (each element less than or equal to the next).
*/
public void isInOrder();
/**
* Fails if the array is not in order according to the given comparator.
* @param comparator the comparator to use for ordering comparison
*/
public void isInOrder(Comparator<? super T> comparator);
/**
* Fails if the array is not in strictly increasing natural order (each element strictly less than the next).
*/
public void isInStrictOrder();
/**
* Fails if the array is not in strictly increasing order according to the given comparator.
* @param comparator the comparator to use for ordering comparison
*/
public void isInStrictOrder(Comparator<? super T> comparator);Usage Examples:
Integer[] numbers = {1, 2, 3, 4, 5};
assertThat(numbers).isInOrder();
assertThat(numbers).isInStrictOrder();
String[] words = {"apple", "banana", "cherry"};
assertThat(words).isInOrder();
// Custom ordering
String[] byLength = {"cat", "dog", "elephant"};
assertThat(byLength).isInOrder(Comparator.comparing(String::length));Truth provides specialized subjects for all primitive array types with appropriate type-safe methods.
/**
* Creates a PrimitiveBooleanArraySubject for asserting about boolean arrays.
* @param actual the boolean array under test
*/
public static PrimitiveBooleanArraySubject assertThat(boolean[] actual);Usage Examples:
boolean[] flags = {true, false, true};
assertThat(flags).hasLength(3);
assertThat(flags).contains(true);
assertThat(flags).containsExactly(true, false, true).inOrder();/**
* Creates a PrimitiveIntArraySubject for asserting about int arrays.
* @param actual the int array under test
*/
public static PrimitiveIntArraySubject assertThat(int[] actual);Usage Examples:
int[] numbers = {1, 2, 3, 4, 5};
assertThat(numbers).hasLength(5);
assertThat(numbers).contains(3);
assertThat(numbers).containsExactly(1, 2, 3, 4, 5).inOrder();
assertThat(numbers).isInOrder();
assertThat(numbers).isInStrictOrder();/**
* Creates a PrimitiveLongArraySubject for asserting about long arrays.
* @param actual the long array under test
*/
public static PrimitiveLongArraySubject assertThat(long[] actual);Usage Examples:
long[] timestamps = {1000000L, 2000000L, 3000000L};
assertThat(timestamps).hasLength(3);
assertThat(timestamps).isInStrictOrder();
assertThat(timestamps).containsAtLeast(1000000L, 3000000L);/**
* Creates a PrimitiveDoubleArraySubject for asserting about double arrays.
* @param actual the double array under test
*/
public static PrimitiveDoubleArraySubject assertThat(double[] actual);
/**
* Returns a tolerance-based comparison for double arrays.
* @param tolerance the tolerance for floating-point comparisons
*/
public TolerantPrimitiveDoubleArrayComparison usingTolerance(double tolerance);Usage Examples:
double[] measurements = {1.1, 2.2, 3.3};
assertThat(measurements).hasLength(3);
assertThat(measurements).isInStrictOrder();
// Tolerance-based comparisons for floating-point precision
double[] calculated = {1.0000001, 2.0000002, 3.0000003};
double[] expected = {1.0, 2.0, 3.0};
assertThat(calculated).usingTolerance(0.001).containsExactlyElementsIn(expected).inOrder();/**
* Creates a PrimitiveFloatArraySubject for asserting about float arrays.
* @param actual the float array under test
*/
public static PrimitiveFloatArraySubject assertThat(float[] actual);
/**
* Returns a tolerance-based comparison for float arrays.
* @param tolerance the tolerance for floating-point comparisons
*/
public TolerantPrimitiveFloatArrayComparison usingTolerance(float tolerance);Usage Examples:
float[] coordinates = {1.5f, 2.7f, 3.9f};
assertThat(coordinates).hasLength(3);
assertThat(coordinates).isInOrder();
// Tolerance-based comparisons
float[] actual = {1.00001f, 2.00002f};
float[] expected = {1.0f, 2.0f};
assertThat(actual).usingTolerance(0.001f).containsExactlyElementsIn(expected);/**
* Creates a PrimitiveByteArraySubject for asserting about byte arrays.
* @param actual the byte array under test
*/
public static PrimitiveByteArraySubject assertThat(byte[] actual);
/**
* Creates a PrimitiveCharArraySubject for asserting about char arrays.
* @param actual the char array under test
*/
public static PrimitiveCharArraySubject assertThat(char[] actual);
/**
* Creates a PrimitiveShortArraySubject for asserting about short arrays.
* @param actual the short array under test
*/
public static PrimitiveShortArraySubject assertThat(short[] actual);Usage Examples:
// Byte arrays - useful for binary data
byte[] data = {0x01, 0x02, 0x03, 0x04};
assertThat(data).hasLength(4);
assertThat(data).contains((byte) 0x02);
// Character arrays - useful for string-like data
char[] letters = {'a', 'b', 'c'};
assertThat(letters).hasLength(3);
assertThat(letters).isInOrder();
assertThat(letters).containsExactly('a', 'b', 'c').inOrder();
// Short arrays
short[] values = {100, 200, 300};
assertThat(values).isInStrictOrder();
assertThat(values).containsAtLeast((short) 100, (short) 300);Real-world examples of array testing patterns and best practices.
// Testing 2D arrays
int[][] matrix = {{1, 2}, {3, 4}};
assertThat(matrix).hasLength(2);
assertThat(matrix[0]).containsExactly(1, 2).inOrder();
assertThat(matrix[1]).containsExactly(3, 4).inOrder();
// Testing jagged arrays
String[][] jaggedArray = {{"a"}, {"b", "c"}, {"d", "e", "f"}};
assertThat(jaggedArray).hasLength(3);
assertThat(jaggedArray[0]).hasLength(1);
assertThat(jaggedArray[1]).hasLength(2);
assertThat(jaggedArray[2]).hasLength(3);// Testing array contents after transformations
String[] names = {"Alice", "Bob", "Charlie"};
String[] upperNames = Arrays.stream(names)
.map(String::toUpperCase)
.toArray(String[]::new);
assertThat(upperNames).containsExactly("ALICE", "BOB", "CHARLIE").inOrder();
// Testing filtered arrays
int[] numbers = {1, 2, 3, 4, 5, 6};
int[] evenNumbers = Arrays.stream(numbers)
.filter(n -> n % 2 == 0)
.toArray();
assertThat(evenNumbers).containsExactly(2, 4, 6).inOrder();// Custom comparison using Correspondence
Correspondence<String, String> IGNORING_CASE =
Correspondence.from((actual, expected) ->
actual.toLowerCase().equals(expected.toLowerCase()),
"equals ignoring case");
String[] actual = {"Hello", "WORLD"};
String[] expected = {"hello", "world"};
// Note: This would require custom extension - Truth's array subjects
// don't directly support Correspondence, but you can convert to lists
assertThat(Arrays.asList(actual))
.comparingElementsUsing(IGNORING_CASE)
.containsExactlyElementsIn(Arrays.asList(expected));// Testing large array properties
int[] largeArray = new int[1000000];
Arrays.fill(largeArray, 42);
assertThat(largeArray).hasLength(1000000);
assertThat(largeArray[0]).isEqualTo(42);
assertThat(largeArray[999999]).isEqualTo(42);
// Testing sorted array properties
int[] sortedData = IntStream.range(0, 10000).toArray();
assertThat(sortedData).isInStrictOrder();
assertThat(sortedData).contains(5000);/**
* Subject class for making assertions about object arrays.
* @param <T> the type of array elements
*/
public class ObjectArraySubject<T> extends Subject {
/**
* Constructor for ObjectArraySubject.
* @param metadata failure metadata for context
* @param actual the array under test
*/
protected ObjectArraySubject(FailureMetadata metadata, T[] actual);
}
/**
* Subject class for making assertions about boolean arrays.
*/
public class PrimitiveBooleanArraySubject extends Subject {
protected PrimitiveBooleanArraySubject(FailureMetadata metadata, boolean[] actual);
}
/**
* Subject class for making assertions about int arrays.
*/
public class PrimitiveIntArraySubject extends Subject {
protected PrimitiveIntArraySubject(FailureMetadata metadata, int[] actual);
}
/**
* Subject class for making assertions about long arrays.
*/
public class PrimitiveLongArraySubject extends Subject {
protected PrimitiveLongArraySubject(FailureMetadata metadata, long[] actual);
}
/**
* Subject class for making assertions about double arrays with tolerance support.
*/
public class PrimitiveDoubleArraySubject extends Subject {
protected PrimitiveDoubleArraySubject(FailureMetadata metadata, double[] actual);
/**
* Returns a tolerance-based comparison for double arrays.
* @param tolerance the tolerance for floating-point comparisons
*/
public TolerantPrimitiveDoubleArrayComparison usingTolerance(double tolerance);
}
/**
* Subject class for making assertions about float arrays with tolerance support.
*/
public class PrimitiveFloatArraySubject extends Subject {
protected PrimitiveFloatArraySubject(FailureMetadata metadata, float[] actual);
/**
* Returns a tolerance-based comparison for float arrays.
* @param tolerance the tolerance for floating-point comparisons
*/
public TolerantPrimitiveFloatArrayComparison usingTolerance(float tolerance);
}
/**
* Subject class for making assertions about byte arrays.
*/
public class PrimitiveByteArraySubject extends Subject {
protected PrimitiveByteArraySubject(FailureMetadata metadata, byte[] actual);
}
/**
* Subject class for making assertions about char arrays.
*/
public class PrimitiveCharArraySubject extends Subject {
protected PrimitiveCharArraySubject(FailureMetadata metadata, char[] actual);
}
/**
* Subject class for making assertions about short arrays.
*/
public class PrimitiveShortArraySubject extends Subject {
protected PrimitiveShortArraySubject(FailureMetadata metadata, short[] actual);
}
/**
* Provides tolerance-based comparison methods for double arrays.
*/
public class TolerantPrimitiveDoubleArrayComparison {
/**
* Fails if the array does not contain exactly the given elements within tolerance.
* @param expected the expected elements
*/
public Ordered containsExactly(double... expected);
/**
* Fails if the array does not contain exactly the elements in the given iterable within tolerance.
* @param expected the iterable containing expected elements
*/
public Ordered containsExactlyElementsIn(Iterable<Double> expected);
}
/**
* Provides tolerance-based comparison methods for float arrays.
*/
public class TolerantPrimitiveFloatArrayComparison {
/**
* Fails if the array does not contain exactly the given elements within tolerance.
* @param expected the expected elements
*/
public Ordered containsExactly(float... expected);
/**
* Fails if the array does not contain exactly the elements in the given iterable within tolerance.
* @param expected the iterable containing expected elements
*/
public Ordered containsExactlyElementsIn(Iterable<Float> expected);
}
/**
* Interface returned by containsExactly methods to enforce ordering constraints.
*/
public interface Ordered {
/**
* Requires that the elements appear in the given order.
*/
void inOrder();
}Install with Tessl CLI
npx tessl i tessl/maven-com-google-truth--truth