Skip to content

assertj/assertj-vavr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

243 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

build Maven Central Version javadoc

assertj-vavr

AssertJ assertions for Vavr types.

Installation

Gradle:

testImplementation "org.assertj:assertj-vavr:$version"

Maven:

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-vavr</artifactId>
    <version>${version}</version>
    <scope>test</scope>
</dependency>

Usage

All assertions are available via the VavrAssertions.assertThat() static factory:

import static org.assertj.vavr.api.VavrAssertions.assertThat;

Supported types

Vavr type Assertion class
Option<T> OptionAssert
Either<L, R> EitherAssert
Try<T> TryAssert
Validation<E, T> ValidationAssert
Lazy<T> LazyAssert
Seq<T> SeqAssert
Set<T> SetAssert
Map<K, V> MapAssert
Multimap<K, V> MultimapAssert

Option

assertThat(Option.of("hello")).isDefined();
assertThat(Option.none()).isEmpty();
assertThat(Option.of("hello")).contains("hello");
assertThat(Option.of("hello")).containsInstanceOf(String.class);
assertThat(Option.of("hello")).hasValueSatisfying(v -> assertThat(v).startsWith("he"));

Either

assertThat(Either.right("ok")).isRight();
assertThat(Either.left("error")).isLeft();
assertThat(Either.right("ok")).containsRight("ok");
assertThat(Either.left("error")).containsLeft("error");
assertThat(Either.right("ok")).hasRightValueSatisfying(v -> assertThat(v).isEqualTo("ok"));

Try

assertThat(Try.success("ok")).isSuccess();
assertThat(Try.failure(new RuntimeException())).isFailure();
assertThat(Try.success("ok")).contains("ok");
assertThat(Try.success("ok")).hasValueSatisfying(v -> assertThat(v).isEqualTo("ok"));
assertThat(Try.failure(new RuntimeException("oops"))).failReasonHasMessage("oops");

Validation

assertThat(Validation.valid("ok")).isValid();
assertThat(Validation.invalid("error")).isInvalid();
assertThat(Validation.valid("ok")).containsValid("ok");
assertThat(Validation.invalid("error")).containsInvalid("error");

Lazy

assertThat(Lazy.of(() -> "value")).isNotEvaluated();

Lazy<String> evaluated = Lazy.of(() -> "value");
evaluated.get();
assertThat(evaluated).isEvaluated();

Seq

assertThat(List.of(1, 2, 3)).contains(1, Index.atIndex(0));
assertThat(List.of(1, 2, 3)).isSorted();
assertThat(List.of(3, 1, 2)).containsExactlyInAnyOrder(1, 2, 3);

Map

assertThat(HashMap.of("key", "value")).containsKey("key");
assertThat(HashMap.of("key", "value")).containsValue("value");
assertThat(HashMap.of("key", "value")).containsEntry("key", "value");
assertThat(HashMap.empty()).isEmpty();

Soft assertions

Use SoftVavrAssertions to collect all failures instead of stopping at the first one:

SoftVavrAssertions softly = new SoftVavrAssertions();
softly.assertThat(Option.of("hello")).contains("hello");
softly.assertThat(Try.success("ok")).isSuccess();
softly.assertAll();

Assumptions

Use VavrAssumptions to skip tests when preconditions are not met:

import static org.assertj.vavr.api.VavrAssumptions.assumeThat;

assumeThat(Option.of("hello")).isDefined(); // test is skipped if Option is empty

Contributing

Contributions are welcome. Please follow the assertj-core contributing guide.

Contributors