Skip to content

Asserting methods to return Assert<T> instead of Unit #346

Answered by evant
valeriyo asked this question in Q&A
Discussion options

You must be logged in to vote

While method chaining can be useful it can also be confusing when it's used it different ways in different places. We chose to use chaining only to narrow what's being asserted on. This way each method in the chain is relevant to the one before it. For example:

assertThat(person).isNotNull().prop(Person::name).isEqualTo(...)

We go Person? -> Person -> String.

As you mentioned, when you want to have multiple assertions on the same value, you can use all and it's clear what you are doing and still quite concise

assertThat(array).all {
  hasSize(2)
  isEqualTo(...)
}

One note: all and it's counterpart assertAll do work differently than a chain in assertj in that they use soft assertions. So if

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@valeriyo
Comment options

Answer selected by evant
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #345 on April 21, 2021 14:22.