diff --git a/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java b/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java index e4ee98e8a0..2359b13847 100644 --- a/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java +++ b/spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java @@ -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 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; } } diff --git a/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java index b1e6ced4b1..bc3dc33b09 100644 --- a/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java @@ -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]"); }