From da0318d37ec8a4ba75b29375aa0d282ee4b40eaf Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sat, 23 May 2026 10:03:36 +0200 Subject: [PATCH] Fix Multimaps.assertContainsExactly order check with duplicate keys --- .../org/assertj/vavr/internal/Multimaps.java | 5 ++- .../MultimapAssert_containsExactly_Test.java | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/assertj/vavr/internal/Multimaps.java b/src/main/java/org/assertj/vavr/internal/Multimaps.java index a54e05e..9d1df69 100644 --- a/src/main/java/org/assertj/vavr/internal/Multimaps.java +++ b/src/main/java/org/assertj/vavr/internal/Multimaps.java @@ -185,9 +185,8 @@ public void assertContainsExactly(AssertionInfo info, Multimap actu if (notExpected.isEmpty() && notFound.isEmpty()) { // check entries order int index = 0; - for (K keyFromActual : actual.keySet()) { - if (areNotEqual(keyFromActual, entries[index]._1)) { - Tuple2> actualEntry = Tuple.of(keyFromActual, actual.get(keyFromActual).get()); + for (Tuple2 actualEntry : actual) { + if (!deepEquals(actualEntry._1, entries[index]._1) || !deepEquals(actualEntry._2, entries[index]._2)) { throw failures.failure(info, elementsDifferAtIndex(actualEntry, entries[index], index)); } index++; diff --git a/src/test/java/org/assertj/vavr/api/MultimapAssert_containsExactly_Test.java b/src/test/java/org/assertj/vavr/api/MultimapAssert_containsExactly_Test.java index 175efee..e14ff5c 100644 --- a/src/test/java/org/assertj/vavr/api/MultimapAssert_containsExactly_Test.java +++ b/src/test/java/org/assertj/vavr/api/MultimapAssert_containsExactly_Test.java @@ -18,6 +18,7 @@ import io.vavr.Tuple; import io.vavr.Tuple2; import io.vavr.collection.HashMultimap; +import io.vavr.collection.LinkedHashMultimap; import io.vavr.collection.Multimap; import org.junit.jupiter.api.Test; @@ -136,9 +137,38 @@ void should_fail_if_Multimap_does_not_contain_all_entries_in_same_order() { .isInstanceOf(AssertionError.class) .hasMessage( "\nActual and expected have the same elements but not in the same order, at index 0 actual element was:\n" + - " (key1, List(value1))\n" + + " (key1, value1)\n" + "whereas expected element was:\n" + " (key3, value3)\n" ); } + + @Test + void should_pass_if_Multimap_with_duplicate_keys_contains_entries_in_order() { + Multimap actual = LinkedHashMultimap.withSeq().of( + "key1", "value1", "key1", "value2", "key2", "value3" + ); + + assertThat(actual).containsExactly( + Tuple.of("key1", "value1"), + Tuple.of("key1", "value2"), + Tuple.of("key2", "value3") + ); + } + + @Test + void should_fail_if_Multimap_with_duplicate_keys_has_entries_in_wrong_order() { + Multimap actual = LinkedHashMultimap.withSeq().of( + "key1", "value1", "key1", "value2", "key2", "value3" + ); + + assertThatThrownBy( + () -> assertThat(actual).containsExactly( + Tuple.of("key1", "value2"), + Tuple.of("key1", "value1"), + Tuple.of("key2", "value3") + ) + ) + .isInstanceOf(AssertionError.class); + } }