Skip to content

Commit 530bb44

Browse files
committed
Fix stack overflow error in assertIterableEquals
Prior to this commit an iterable of iterables (here instances of java.nio.file.Path) passed to method `assertIterableEquals` in class `Assertions` of the JUnit Jupiter API yielded a `StackOverflowError`. This commit prevent the eternal while-loop by leveraging the `equals()`-implementation of the non-null expected element. Fixes #2157
1 parent 12ee207 commit 530bb44

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

junit-jupiter-api/src/main/java/org/junit/jupiter/api/AssertIterableEquals.java

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ private static void assertIterableEquals(Iterable<?> expected, Iterable<?> actua
6767
if (expectedElement == actualElement) {
6868
continue;
6969
}
70+
// Prevent stack overflow error.
71+
// See https://github.com/junit-team/junit5/issues/2157 for details.
72+
if (expectedElement != null && expectedElement.equals(actualElement)) {
73+
continue;
74+
}
7075

7176
indexes.addLast(processed - 1);
7277
assertIterableElementsEqual(expectedElement, actualElement, indexes, messageOrSupplier);

junit-jupiter-engine/src/test/java/org/junit/jupiter/api/AssertIterableEqualsAssertionsTests.java

+10
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals;
1515
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageStartsWith;
1616
import static org.junit.jupiter.api.AssertionTestUtils.expectAssertionFailedError;
17+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1718
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
1819
import static org.junit.jupiter.api.IterableFactory.listOf;
1920
import static org.junit.jupiter.api.IterableFactory.setOf;
2021

22+
import java.nio.file.Path;
2123
import java.util.List;
2224
import java.util.Set;
2325

@@ -439,4 +441,12 @@ void assertIterableEqualsDifferentNestedIterablesAndMessageSupplier() {
439441
}
440442
}
441443

444+
@Test
445+
// https://github.com/junit-team/junit5/issues/2157
446+
void assertIterableEqualsWithListOfPath() {
447+
var expected = listOf(Path.of("1"));
448+
var actual = listOf(Path.of("1"));
449+
assertDoesNotThrow(() -> assertIterableEquals(expected, actual));
450+
}
451+
442452
}

0 commit comments

Comments
 (0)