Fluent assertion framework for Java that provides clear, readable test assertions and informative failure messages
—
Type-safe numeric assertions with comparison operations and tolerance-based floating-point comparisons.
/**
* Creates an IntegerSubject for asserting about Integer values.
* @param actual the Integer under test
*/
public static IntegerSubject assertThat(Integer actual);
/**
* Creates a LongSubject for asserting about Long values.
* @param actual the Long under test
*/
public static LongSubject assertThat(Long actual);
/**
* Creates a DoubleSubject for asserting about Double values.
* @param actual the Double under test
*/
public static DoubleSubject assertThat(Double actual);
/**
* Creates a FloatSubject for asserting about Float values.
* @param actual the Float under test
*/
public static FloatSubject assertThat(Float actual);
/**
* Creates a BigDecimalSubject for asserting about BigDecimal values.
* @param actual the BigDecimal under test
*/
public static BigDecimalSubject assertThat(BigDecimal actual);All numeric subjects inherit from ComparableSubject providing comparison operations.
/**
* Fails if the subject is not greater than or equal to the given value.
* @param other the value to compare against
*/
public void isAtLeast(T other);
/**
* Fails if the subject is not less than or equal to the given value.
* @param other the value to compare against
*/
public void isAtMost(T other);
/**
* Fails if the subject is not strictly greater than the given value.
* @param other the value to compare against
*/
public void isGreaterThan(T other);
/**
* Fails if the subject is not strictly less than the given value.
* @param other the value to compare against
*/
public void isLessThan(T other);Usage Examples:
// Integer comparisons
assertThat(42).isGreaterThan(30);
assertThat(42).isAtLeast(42);
assertThat(42).isAtMost(50);
assertThat(42).isLessThan(100);
// Long comparisons
assertThat(1000L).isGreaterThan(999L);
assertThat(1000L).isAtLeast(1000L);
// BigDecimal comparisons
BigDecimal price = new BigDecimal("19.99");
assertThat(price).isGreaterThan(new BigDecimal("15.00"));
assertThat(price).isLessThan(new BigDecimal("25.00"));/**
* Fails if the subject is not within the given range (Guava Range).
* @param range the range that should contain the subject
*/
public void isIn(Range<T> range);
/**
* Fails if the subject is within the given range.
* @param range the range that should not contain the subject
*/
public void isNotIn(Range<T> range);Usage Examples:
import com.google.common.collect.Range;
// Range checking
assertThat(5).isIn(Range.closed(1, 10)); // [1, 10]
assertThat(5).isIn(Range.open(0, 10)); // (0, 10)
assertThat(5).isIn(Range.atLeast(5)); // [5, +∞)
assertThat(5).isNotIn(Range.greaterThan(10)); // (10, +∞)Special handling for floating-point comparisons with tolerance.
/**
* Prepares for a tolerance-based comparison of double values.
* @param tolerance the allowed difference between actual and expected
*/
public TolerantDoubleComparison isWithin(double tolerance);
/**
* Prepares for a tolerance-based comparison of float values.
* @param tolerance the allowed difference between actual and expected
*/
public TolerantFloatComparison isWithin(float tolerance);/**
* Fails if the subject is not equal to the given value within the tolerance.
* @param expected the expected value
*/
public void of(double expected);
/**
* Fails if the subject is not equal to the given Double within the tolerance.
* @param expected the expected Double value
*/
public void of(Double expected);
/**
* Fails if the subject is not equal to the given int within the tolerance.
* @param expected the expected int value
*/
public void of(int expected);
/**
* Fails if the subject is not equal to the given long within the tolerance.
* @param expected the expected long value
*/
public void of(long expected);/**
* Fails if the subject is not equal to the given value within the tolerance.
* @param expected the expected value
*/
public void of(float expected);
/**
* Fails if the subject is not equal to the given Float within the tolerance.
* @param expected the expected Float value
*/
public void of(Float expected);
/**
* Fails if the subject is not equal to the given int within the tolerance.
* @param expected the expected int value
*/
public void of(int expected);
/**
* Fails if the subject is not equal to the given long within the tolerance.
* @param expected the expected long value
*/
public void of(long expected);Usage Examples:
// Double tolerance comparisons
double result = 1.0 / 3.0;
assertThat(result).isWithin(0.0001).of(0.3333);
assertThat(result).isWithin(1e-10).of(1.0 / 3.0);
// Float tolerance comparisons
float calculation = 0.1f + 0.2f;
assertThat(calculation).isWithin(0.0001f).of(0.3f);
// Comparing with different numeric types
assertThat(3.14159).isWithin(0.001).of(Math.PI);
assertThat(2.0).isWithin(0.1).of(2); // int
assertThat(2.0).isWithin(0.1).of(2L); // longSpecial assertions for handling infinite and NaN values.
/**
* Fails if the subject is not positive infinity.
*/
public void isPositiveInfinity();
/**
* Fails if the subject is not negative infinity.
*/
public void isNegativeInfinity();
/**
* Fails if the subject is not NaN (Not a Number).
*/
public void isNaN();
/**
* Fails if the subject is NaN.
*/
public void isNotNaN();
/**
* Fails if the subject is not finite (i.e., is infinite or NaN).
*/
public void isFinite();Usage Examples:
// Infinity checks
assertThat(Double.POSITIVE_INFINITY).isPositiveInfinity();
assertThat(Double.NEGATIVE_INFINITY).isNegativeInfinity();
assertThat(1.0 / 0.0).isPositiveInfinity();
// NaN checks
assertThat(Double.NaN).isNaN();
assertThat(0.0 / 0.0).isNaN();
assertThat(Math.sqrt(-1)).isNaN();
assertThat(42.0).isNotNaN();
// Finite checks
assertThat(42.0).isFinite();
assertThat(Math.PI).isFinite();Special methods for BigDecimal comparisons.
/**
* Fails if the subject is not equal to the given BigDecimal ignoring scale differences.
* For example, 2.0 and 2.00 are considered equal.
* @param expected the expected BigDecimal value
*/
public void isEqualToIgnoringScale(BigDecimal expected);
/**
* Fails if the subject is equal to the given BigDecimal ignoring scale differences.
* @param unexpected the BigDecimal value that should not be equal ignoring scale
*/
public void isNotEqualToIgnoringScale(BigDecimal unexpected);Usage Examples:
BigDecimal value1 = new BigDecimal("2.0");
BigDecimal value2 = new BigDecimal("2.00");
BigDecimal value3 = new BigDecimal("2.000");
// These would fail with regular isEqualTo due to scale differences
assertThat(value1).isEqualToIgnoringScale(value2);
assertThat(value2).isEqualToIgnoringScale(value3);
assertThat(value1).isNotEqualToIgnoringScale(new BigDecimal("3.0"));
// Regular equality considers scale
assertThat(value1).isNotEqualTo(value2); // Different scalesCommon patterns for numeric testing:
// Testing calculations with tolerance
double calculateCircleArea(double radius) {
return Math.PI * radius * radius;
}
double area = calculateCircleArea(5.0);
assertThat(area).isWithin(0.001).of(78.539);
// Testing ranges and bounds
int score = calculateScore();
assertThat(score).isAtLeast(0);
assertThat(score).isAtMost(100);
assertThat(score).isIn(Range.closed(0, 100));
// Testing statistical calculations
List<Double> values = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);
double average = values.stream().mapToDouble(Double::doubleValue).average().orElse(0.0);
assertThat(average).isWithin(0.0001).of(3.0);
// Testing financial calculations
BigDecimal price = new BigDecimal("19.99");
BigDecimal tax = new BigDecimal("0.08");
BigDecimal total = price.multiply(BigDecimal.ONE.add(tax));
assertThat(total).isEqualToIgnoringScale(new BigDecimal("21.5892"));
// Testing edge cases
double result = Math.log(-1);
assertThat(result).isNaN();
double division = 1.0 / 0.0;
assertThat(division).isPositiveInfinity();/**
* Base subject class for comparable types providing comparison methods.
*/
public abstract class ComparableSubject<T extends Comparable<?>> extends Subject {
/**
* Constructor for ComparableSubject.
* @param metadata failure metadata for context
* @param actual the Comparable value under test
*/
protected ComparableSubject(FailureMetadata metadata, T actual);
}
/**
* Subject class for making assertions about Integer values.
*/
public class IntegerSubject extends ComparableSubject<Integer> {
/**
* Constructor for IntegerSubject.
* @param metadata failure metadata for context
* @param actual the Integer under test
*/
protected IntegerSubject(FailureMetadata metadata, Integer actual);
}
/**
* Subject class for making assertions about Long values.
*/
public class LongSubject extends ComparableSubject<Long> {
/**
* Constructor for LongSubject.
* @param metadata failure metadata for context
* @param actual the Long under test
*/
protected LongSubject(FailureMetadata metadata, Long actual);
}
/**
* Subject class for making assertions about Double values.
*/
public class DoubleSubject extends ComparableSubject<Double> {
/**
* Constructor for DoubleSubject.
* @param metadata failure metadata for context
* @param actual the Double under test
*/
protected DoubleSubject(FailureMetadata metadata, Double actual);
}
/**
* Subject class for making assertions about Float values.
*/
public class FloatSubject extends ComparableSubject<Float> {
/**
* Constructor for FloatSubject.
* @param metadata failure metadata for context
* @param actual the Float under test
*/
protected FloatSubject(FailureMetadata metadata, Float actual);
}
/**
* Subject class for making assertions about BigDecimal values.
*/
public class BigDecimalSubject extends ComparableSubject<BigDecimal> {
/**
* Constructor for BigDecimalSubject.
* @param metadata failure metadata for context
* @param actual the BigDecimal under test
*/
protected BigDecimalSubject(FailureMetadata metadata, BigDecimal actual);
}
/**
* Provides tolerance-based comparison methods for double values.
*/
public abstract class TolerantDoubleComparison {
// Methods documented above
}
/**
* Provides tolerance-based comparison methods for float values.
*/
public abstract class TolerantFloatComparison {
// Methods documented above
}Install with Tessl CLI
npx tessl i tessl/maven-com-google-truth--truth