Close

AssertJ - Number Assertions

[Last Updated: Mar 3, 2026]

Numbers are ubiquitous in Java applications, and AssertJ provides assertion methods tailored to numeric types including int, long, double, float, and BigDecimal. These go beyond equality to support range, sign, and approximate comparisons.

Floating Point Comparisons

Floating point arithmetic is inherently imprecise. AssertJ's isCloseTo(expected, offset) and isCloseTo(expected, within(delta)) methods handle this gracefully, letting you assert approximate equality without brittle exact comparisons.

Key Number Assertion Methods

isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isBetween, isPositive, isNegative, isZero, isCloseTo.

Example

package com.logicbig.example;

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import static org.assertj.core.api.Assertions.*;

public class NumberAssertionsExample {

    @Test
    void myTest() {
        // integer range checks
        assertThat(42).isGreaterThan(10)
                      .isLessThanOrEqualTo(42)
                      .isBetween(1, 100);
        System.out.println("Integer range passed");

        // sign checks
        assertThat(-5).isNegative();
        assertThat(0).isZero();
        assertThat(7).isPositive();
        System.out.println("Sign checks passed");

        // floating point with offset
        assertThat(3.14159).isCloseTo(3.14, within(0.01));
        System.out.println("Double closeTo passed");

        // BigDecimal
        assertThat(new BigDecimal("9.99")).isGreaterThan(new BigDecimal("5.00"))
                                          .isLessThan(new BigDecimal("10.00"));
        System.out.println("BigDecimal checks passed");
    }
}

Output

$ mvn clean test -Dtest=*
[INFO] Scanning for projects...
[INFO]
[INFO] -----------< com.logicbig.example:assertj-number-assertions >-----------
[INFO] Building assertj-number-assertions 1.0-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ assertj-number-assertions ---
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ assertj-number-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-number-assertions\src\main\resources
[INFO]
[INFO] --- compiler:3.11.0:compile (default-compile) @ assertj-number-assertions ---
[INFO] No sources to compile
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ assertj-number-assertions ---
[INFO] skip non existing resourceDirectory D:\example-projects\assertj\assertj-number-assertions\src\test\resources
[INFO]
[INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ assertj-number-assertions ---
[INFO] Changes detected - recompiling the module! :source
[INFO] Compiling 1 source file with javac [debug target 17] to target\test-classes
[INFO]
[INFO] --- surefire:3.2.5:test (default-test) @ assertj-number-assertions ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[WARNING] file.encoding cannot be set as system property, use <argLine>-Dfile.encoding=...</argLine> instead
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.logicbig.example.NumberAssertionsExample
Integer range passed
Sign checks passed
Double closeTo passed
BigDecimal checks passed
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.132 s -- in com.logicbig.example.NumberAssertionsExample
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.693 s
[INFO] Finished at: 2026-03-03T13:36:33+08:00
[INFO] ------------------------------------------------------------------------

Conclusion

The output confirms that range checks, sign checks, and approximate floating-point comparisons all pass. Notably, isCloseTo with within(0.01) handles the imprecision inherent in floating-point arithmetic, which is the recommended approach when testing calculated decimal values.

Example Project

Dependencies and Technologies Used:

  • assertj-core 3.27.7 (Rich and fluent assertions for testing in Java)
  • junit-jupiter-engine 6.0.2 (Module "junit-jupiter-engine" of JUnit)
  • JDK 17
  • Maven 3.9.11

AssertJ - Number Assertions Select All Download
  • assertj-number-assertions
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • NumberAssertionsExample.java

    See Also

    Join