AssertJ Integration
Runnable example:
AssertJSampleTest.java
The fun-assertj module provides fluent AssertJ custom assertions for all
dmx-fun types. It is an optional test-only dependency — the core fun
library has no runtime dependency on AssertJ.
Adding the dependency
fun-assertj declares assertj-core as compileOnly. You must add
assertj-core explicitly to your own test classpath. Any version from
3.21.x through 3.27.x is supported (see version compatibility below).
// Gradle — test scope onlytestImplementation("codes.domix:fun-assertj:0.0.13")// AssertJ itself — bring your own version (3.21.x – 3.27.x)testImplementation("org.assertj:assertj-core:3.27.7")<!-- Maven — test scope only --><dependency> <groupId>codes.domix</groupId> <artifactId>fun-assertj</artifactId> <version>0.0.13</version> <scope>test</scope></dependency><dependency> <groupId>org.assertj</groupId> <artifactId>assertj-core</artifactId> <version>3.27.7</version> <scope>test</scope></dependency>Entry point
All assertions are accessed through a single overloaded static method:
import static dmx.fun.assertj.DmxFunAssertions.assertThat;DmxFunAssertions.assertThat coexists with
org.assertj.core.api.Assertions.assertThat — both can be statically imported
in the same test class without naming conflict, because the argument types differ.
Note:
fun-assertjcannot be used inside the:libmodule’s own tests due to a circular dependency. Use it only in consumer projects or in the:assertjmodule’s own test suite.
Assertions reference
| Type | Methods |
|---|---|
Option<T> | isSome(), isNone(), containsValue(expected), hasValueSatisfying(consumer) |
Result<V, E> | isOk(), isErr(), containsValue(expected), containsError(expected) |
Try<V> | isSuccess(), isFailure(), containsValue(expected), failsWith(exceptionType) |
Validated<E, A> | isValid(), isInvalid(), containsValue(expected), hasError(expected) |
Tuple2<A, B> | hasFirst(expected), hasSecond(expected) |
Tuple3<A, B, C> | hasFirst(expected), hasSecond(expected), hasThird(expected) |
Tuple4<A, B, C, D> | hasFirst(expected), hasSecond(expected), hasThird(expected), hasFourth(expected) |
Option<T> assertions
import static dmx.fun.assertj.DmxFunAssertions.assertThat;
// Assert presence and valueassertThat(Option.some(42)) .isSome() .containsValue(42);
// Assert absenceassertThat(Option.<String>none()) .isNone();
// Assert the value satisfies a condition without extracting itassertThat(Option.some("hello")) .isSome() .hasValueSatisfying(v -> assertThat(v).startsWith("hel").hasSize(5));Result<V, E> assertions
import static dmx.fun.assertj.DmxFunAssertions.assertThat;
// Assert Ok and its valueassertThat(Result.ok("hello")) .isOk() .containsValue("hello");
// Assert Err and its errorassertThat(Result.<String, String>err("oops")) .isErr() .containsError("oops");
// Combining with standard AssertJ — both static imports coexistassertThat(Result.ok(List.of(1, 2, 3))) .isOk() .containsValue(List.of(1, 2, 3));Try<V> assertions
import static dmx.fun.assertj.DmxFunAssertions.assertThat;
// Assert success and valueassertThat(Try.success("data")) .isSuccess() .containsValue("data");
// Assert failure — just check it failedassertThat(Try.failure(new RuntimeException("boom"))) .isFailure();
// Assert failure with a specific exception typeassertThat(Try.of(() -> { throw new IOException("disk full"); })) .isFailure() .failsWith(IOException.class);
// Assert a computation succeedsassertThat(Try.of(() -> Integer.parseInt("42"))) .isSuccess() .containsValue(42);Validated<E, A> assertions
import static dmx.fun.assertj.DmxFunAssertions.assertThat;
// Assert valid and valueassertThat(Validated.valid("ok")) .isValid() .containsValue("ok");
// Assert invalid and errorassertThat(Validated.<String, String>invalid("bad input")) .isInvalid() .hasError("bad input");
// Assert NEL-based validation resultValidated<NonEmptyList<String>, Integer> result = Validated.invalidNel("must be positive");
assertThat(result) .isInvalid() .hasError(NonEmptyList.singleton("must be positive"));Tuple2/3/4 assertions
import static dmx.fun.assertj.DmxFunAssertions.assertThat;
// Tuple2assertThat(new Tuple2<>("Alice", 30)) .hasFirst("Alice") .hasSecond(30);
// Tuple3assertThat(Tuple3.of("Alice", 30, true)) .hasFirst("Alice") .hasSecond(30) .hasThird(true);
// Tuple4assertThat(Tuple4.of("Alice", 30, true, 1.75)) .hasFirst("Alice") .hasSecond(30) .hasThird(true) .hasFourth(1.75);Version compatibility
The module is tested in CI against the following AssertJ versions on every pull
request that touches the assertj/ module:
| AssertJ version | Status |
|---|---|
| 3.21.x | tested |
| 3.22.x | tested |
| 3.23.x | tested |
| 3.24.x | tested |
| 3.25.x | tested |
| 3.26.x | tested |
| 3.27.x | tested |
To run the tests locally against a specific version:
./gradlew :assertj:test -PassertjVersion=3.25.3