Skip to content

Commit 03373ee

Browse files
committed
Fix the rendering of non-assertion-error throwables in verifyEach
1 parent 9603fdc commit 03373ee

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

spock-core/src/main/java/org/spockframework/runtime/SpockRuntime.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public static <T> void verifyEach(
217217
}
218218

219219
private static <T> SpockAssertionError getAssertionFailedError(Function<? super T, ?> namer, InternalItemFailure<T> failure) {
220-
SpockAssertionError error = new SpockAssertionError(String.format("Assertions failed for item[%d] %s:\n%s", failure.index, namer.apply(failure.item), failure.throwable.getMessage()));
220+
SpockAssertionError error = new SpockAssertionError(String.format("Assertions failed for item[%d] %s:\n%s", failure.index, namer.apply(failure.item), failure.throwable.toString()));
221221
error.setStackTrace(failure.throwable.getStackTrace());
222222
return error;
223223
}

spock-specs/src/test/groovy/org/spockframework/smoke/VerifyEachBlocks.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,44 @@ class VerifyEachBlocks extends EmbeddedSpecification {
8383
snap.assertThat(e.message).matchesSnapshot()
8484
}
8585

86+
def "verifyEach fails handles exception"(@Snapshot Snapshotter snap) {
87+
given:
88+
def range = (1..10)
89+
90+
when:
91+
verifyEach(range) {
92+
if (it == 5) {
93+
throw new RuntimeException("x == $it").tap {
94+
// remove stack trace to make snapshot stable
95+
it.stackTrace = []
96+
}
97+
}
98+
}
99+
100+
then:
101+
SpockAssertionError e = thrown()
102+
snap.assertThat(e.message).matchesSnapshot()
103+
}
104+
105+
def "verifyEach fails handles exceptions"(@Snapshot Snapshotter snap) {
106+
given:
107+
def range = (1..10)
108+
109+
when:
110+
verifyEach(range) {
111+
if (it in (5..6)) {
112+
throw new RuntimeException("x == $it").tap {
113+
// remove stack trace to make snapshot stable
114+
it.stackTrace = []
115+
}
116+
}
117+
}
118+
119+
then:
120+
MultipleFailuresError e = thrown()
121+
snap.assertThat(e.message).matchesSnapshot()
122+
}
123+
86124
def "verifyEach fails handles nested exceptions"(@Snapshot Snapshotter snap) {
87125
given:
88126
def range = (1..10)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Assertions failed for item[4] 5:
2+
java.lang.RuntimeException: x == 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Multiple Failures (2 failures)
2+
org.spockframework.runtime.SpockAssertionError: Assertions failed for item[4] 5:
3+
java.lang.RuntimeException: x == 5
4+
org.spockframework.runtime.SpockAssertionError: Assertions failed for item[5] 6:
5+
java.lang.RuntimeException: x == 6

0 commit comments

Comments
 (0)