Skip to content

Commit a1cf6bb

Browse files
committed
Avoid wrapping of AssertionError in ExchangeResult
Closes gh-26303
1 parent fb04047 commit a1cf6bb

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,8 @@
2424
import java.util.List;
2525
import java.util.stream.Collectors;
2626

27+
import org.apache.commons.logging.Log;
28+
import org.apache.commons.logging.LogFactory;
2729
import reactor.core.publisher.Mono;
2830

2931
import org.springframework.http.HttpHeaders;
@@ -54,6 +56,8 @@
5456
*/
5557
public class ExchangeResult {
5658

59+
private static Log logger = LogFactory.getLog(ExchangeResult.class);
60+
5761
private static final List<MediaType> PRINTABLE_MEDIA_TYPES = Arrays.asList(
5862
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
5963
MediaType.parseMediaType("text/*"), MediaType.APPLICATION_FORM_URLENCODED);
@@ -223,7 +227,10 @@ public void assertWithDiagnostics(Runnable assertion) {
223227
assertion.run();
224228
}
225229
catch (AssertionError ex) {
226-
throw new AssertionError(ex.getMessage() + "\n" + this, ex);
230+
if (logger.isErrorEnabled()) {
231+
logger.error("Request details for assertion failure:\n" + this);
232+
}
233+
throw ex;
227234
}
228235
}
229236

spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -100,7 +100,7 @@ void valueMatches() {
100100
// Wrong pattern
101101
assertThatExceptionOfType(AssertionError.class)
102102
.isThrownBy(() -> assertions.valueMatches("Content-Type", ".*ISO-8859-1.*"))
103-
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header " +
103+
.satisfies(ex -> assertThat(ex).hasMessage("Response header " +
104104
"'Content-Type'=[application/json;charset=UTF-8] does not match " +
105105
"[.*ISO-8859-1.*]"));
106106
}
@@ -117,13 +117,13 @@ void valuesMatch() {
117117

118118
assertThatExceptionOfType(AssertionError.class)
119119
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5"))
120-
.satisfies(ex -> assertThat(ex.getCause()).hasMessage(
120+
.satisfies(ex -> assertThat(ex).hasMessage(
121121
"Response header 'foo' has fewer or more values [value1, value2, value3] " +
122122
"than number of patterns to match with [.*, val.*5]"));
123123

124124
assertThatExceptionOfType(AssertionError.class)
125125
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5", ".*"))
126-
.satisfies(ex -> assertThat(ex.getCause()).hasMessage(
126+
.satisfies(ex -> assertThat(ex).hasMessage(
127127
"Response header 'foo'[1]='value2' does not match 'val.*5'"));
128128
}
129129

@@ -158,7 +158,7 @@ void exists() {
158158
// Header should not exist
159159
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
160160
assertions.exists("Framework"))
161-
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header 'Framework' does not exist"));
161+
.satisfies(ex -> assertThat(ex).hasMessage("Response header 'Framework' does not exist"));
162162
}
163163

164164
@Test
@@ -173,7 +173,7 @@ void doesNotExist() {
173173
// Existing header
174174
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
175175
assertions.doesNotExist("Content-Type"))
176-
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header " +
176+
.satisfies(ex -> assertThat(ex).hasMessage("Response header " +
177177
"'Content-Type' exists with value=[application/json;charset=UTF-8]"));
178178
}
179179

@@ -189,7 +189,6 @@ void contentTypeCompatibleWith() {
189189
// MediaTypes not compatible
190190
assertThatExceptionOfType(AssertionError.class)
191191
.isThrownBy(() -> assertions.contentTypeCompatibleWith(MediaType.TEXT_XML))
192-
.havingCause()
193192
.withMessage("Response header 'Content-Type'=[application/xml] is not compatible with [text/xml]");
194193
}
195194

0 commit comments

Comments
 (0)