Skip to content

Commit 9145198

Browse files
authored
Fix the rendering of non-assertion-error throwables in verifyEach (#2163)
1 parent 51c477a commit 9145198

5 files changed

Lines changed: 47 additions & 1 deletion

File tree

docs/release_notes.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ will now require that the spec is annotated with <<parallel_execution.adoc#isola
7777
* OSGi: Pin the `Require-Capability:osgi.ee=JavaSE` to Java `8` spockPull:2233[]
7878
* Fix: Prevent NPE in SpecRunHistory.sortFeatures when duration is missing spockIssue:2234[]
7979
* `Retry` extension does not mesh with `TestAbortedException` and `PendingFeature` spockIssue:1863[]
80+
* Fix display of caught exceptions within `verifyEach` blocks spockPull:2163[]
8081

8182
Thanks to all the contributors to this release: Andreas Turban, Björn Kautler, Christoph Loy, Leonard Brünings, Thanos Tsiamis
8283

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)