Avoid wrapping of AssertionError in ExchangeResult

Closes gh-26303
This commit is contained in:
Rossen Stoyanchev 2021-01-06 21:55:50 +00:00
parent fb040479eb
commit a1cf6bbc37
2 changed files with 15 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,6 +24,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Mono;
import org.springframework.http.HttpHeaders;
@ -54,6 +56,8 @@ import org.springframework.util.MultiValueMap;
*/
public class ExchangeResult {
private static Log logger = LogFactory.getLog(ExchangeResult.class);
private static final List<MediaType> PRINTABLE_MEDIA_TYPES = Arrays.asList(
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML,
MediaType.parseMediaType("text/*"), MediaType.APPLICATION_FORM_URLENCODED);
@ -223,7 +227,10 @@ public class ExchangeResult {
assertion.run();
}
catch (AssertionError ex) {
throw new AssertionError(ex.getMessage() + "\n" + this, ex);
if (logger.isErrorEnabled()) {
logger.error("Request details for assertion failure:\n" + this);
}
throw ex;
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -100,7 +100,7 @@ class HeaderAssertionTests {
// Wrong pattern
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.valueMatches("Content-Type", ".*ISO-8859-1.*"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header " +
.satisfies(ex -> assertThat(ex).hasMessage("Response header " +
"'Content-Type'=[application/json;charset=UTF-8] does not match " +
"[.*ISO-8859-1.*]"));
}
@ -117,13 +117,13 @@ class HeaderAssertionTests {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage(
.satisfies(ex -> assertThat(ex).hasMessage(
"Response header 'foo' has fewer or more values [value1, value2, value3] " +
"than number of patterns to match with [.*, val.*5]"));
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5", ".*"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage(
.satisfies(ex -> assertThat(ex).hasMessage(
"Response header 'foo'[1]='value2' does not match 'val.*5'"));
}
@ -158,7 +158,7 @@ class HeaderAssertionTests {
// Header should not exist
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertions.exists("Framework"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header 'Framework' does not exist"));
.satisfies(ex -> assertThat(ex).hasMessage("Response header 'Framework' does not exist"));
}
@Test
@ -173,7 +173,7 @@ class HeaderAssertionTests {
// Existing header
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
assertions.doesNotExist("Content-Type"))
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header " +
.satisfies(ex -> assertThat(ex).hasMessage("Response header " +
"'Content-Type' exists with value=[application/json;charset=UTF-8]"));
}
@ -189,7 +189,6 @@ class HeaderAssertionTests {
// MediaTypes not compatible
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertions.contentTypeCompatibleWith(MediaType.TEXT_XML))
.havingCause()
.withMessage("Response header 'Content-Type'=[application/xml] is not compatible with [text/xml]");
}