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 only
testImplementation("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-assertj cannot be used inside the :lib module’s own tests due to a circular dependency. Use it only in consumer projects or in the :assertj module’s own test suite.

Assertions reference

TypeMethods
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 value
assertThat(Option.some(42))
.isSome()
.containsValue(42);
// Assert absence
assertThat(Option.<String>none())
.isNone();
// Assert the value satisfies a condition without extracting it
assertThat(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 value
assertThat(Result.ok("hello"))
.isOk()
.containsValue("hello");
// Assert Err and its error
assertThat(Result.<String, String>err("oops"))
.isErr()
.containsError("oops");
// Combining with standard AssertJ — both static imports coexist
assertThat(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 value
assertThat(Try.success("data"))
.isSuccess()
.containsValue("data");
// Assert failure — just check it failed
assertThat(Try.failure(new RuntimeException("boom")))
.isFailure();
// Assert failure with a specific exception type
assertThat(Try.of(() -> { throw new IOException("disk full"); }))
.isFailure()
.failsWith(IOException.class);
// Assert a computation succeeds
assertThat(Try.of(() -> Integer.parseInt("42")))
.isSuccess()
.containsValue(42);

Validated<E, A> assertions

import static dmx.fun.assertj.DmxFunAssertions.assertThat;
// Assert valid and value
assertThat(Validated.valid("ok"))
.isValid()
.containsValue("ok");
// Assert invalid and error
assertThat(Validated.<String, String>invalid("bad input"))
.isInvalid()
.hasError("bad input");
// Assert NEL-based validation result
Validated<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;
// Tuple2
assertThat(new Tuple2<>("Alice", 30))
.hasFirst("Alice")
.hasSecond(30);
// Tuple3
assertThat(Tuple3.of("Alice", 30, true))
.hasFirst("Alice")
.hasSecond(30)
.hasThird(true);
// Tuple4
assertThat(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 versionStatus
3.21.xtested
3.22.xtested
3.23.xtested
3.24.xtested
3.25.xtested
3.26.xtested
3.27.xtested

To run the tests locally against a specific version:

Terminal window
./gradlew :assertj:test -PassertjVersion=3.25.3