assertj / assertj/assertj

Make assertSatisfiesExactlyInAnyOrder() output information about which condition has failed

Open
#3,155 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
2.8k
Forks
788
Avg merge
14h 57m
Merged PRs (30d)
36

Description

#### Feature summary

I just discovered that AssertJ has the `satisfiesExactlyInAnyOrder` and `satisfiesExactly` methods via [this stackoverflow question](https://stackoverflow.com/questions/48042592/asserting-properties-on-list-elements-with-assertj).

I tested them and noticed that the error message when something fails for `satisfiesExactly` is a lot better compared to the one for `satisfiesExactlyInAnyOrder`. I hope this could be improved to also indicate which of the assertions fails like `satisfiesExactly` does (but without the index indication obviously).

#### Example

When running this test:

```java
public class SatisfiesExactlyInAnyOrderTest {

@Test
void testInAnyOrder() {
List starWarsCharacterNames = List.of("Luke", "Leia", "Yoda");
assertThat(starWarsCharacterNames).satisfiesExactlyInAnyOrder(
name -> assertThat(name).contains("Han"),
name -> assertThat(name).contains("L"),
name -> assertThat(name).contains("Y"));
}

@Test
void testExactly() {
List starWarsCharacterNames = List.of("Luke", "Leia", "Yoda");
assertThat(starWarsCharacterNames).satisfiesExactly(
name -> assertThat(name).contains("Han"),
name -> assertThat(name).contains("L"),
name -> assertThat(name).contains("Y"));
}
}
```

This is the output for `testExactly`:

```
java.lang.AssertionError:
Expecting each element of:
["Luke", "Leia", "Yoda"]
to satisfy the requirements at its index, but these elements did not:

"Luke"
- element index: 0
- error:
Expecting actual:
"Luke"
to contain:
"Han"
```

And this the output for `testInAnyOrder`:

```
java.lang.AssertionError:
Expecting actual:
["Luke", "Leia", "Yoda"]
to satisfy all the consumers in any order.
```

I was hoping to get something like this:

```
java.lang.AssertionError:
Expecting actual:
["Luke", "Leia", "Yoda"]
to satisfy all the consumers in any order.

This consumer was never satisfied:
- error:
Expecting actual:
"Luke"
to contain:
"Han"
```

This would make it easier to discover which of the assertions inside of the `satisfiesExactlyInAnyOrder` actually fails.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.