Ensure one time logging for request details

Closes gh-26969
This commit is contained in:
Rossen Stoyanchev
2021-09-06 13:53:30 +01:00
parent 5ea7592d70
commit 1b3fd9edff
2 changed files with 18 additions and 9 deletions

View File

@@ -79,6 +79,9 @@ public class ExchangeResult {
@Nullable @Nullable
private final Object mockServerResult; private final Object mockServerResult;
/** Ensure single logging, e.g. for expectAll. */
private boolean diagnosticsLogged;
/** /**
* Create an instance with an HTTP request and response along with promises * Create an instance with an HTTP request and response along with promises
@@ -121,6 +124,7 @@ public class ExchangeResult {
this.timeout = other.timeout; this.timeout = other.timeout;
this.uriTemplate = other.uriTemplate; this.uriTemplate = other.uriTemplate;
this.mockServerResult = other.mockServerResult; this.mockServerResult = other.mockServerResult;
this.diagnosticsLogged = other.diagnosticsLogged;
} }
@@ -227,7 +231,8 @@ public class ExchangeResult {
assertion.run(); assertion.run();
} }
catch (AssertionError ex) { catch (AssertionError ex) {
if (logger.isErrorEnabled()) { if (!this.diagnosticsLogged && logger.isErrorEnabled()) {
this.diagnosticsLogged = true;
logger.error("Request details for assertion failure:\n" + this); logger.error("Request details for assertion failure:\n" + this);
} }
throw ex; throw ex;

View File

@@ -46,15 +46,19 @@ class SoftAssertionTests {
} }
@Test @Test
void expectAllWithMultipleFailures() throws Exception { void expectAllWithMultipleFailures() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> assertThatExceptionOfType(AssertionError.class)
this.webTestClient.get().uri("/test").exchange() .isThrownBy(() ->
.expectAll( this.webTestClient.get().uri("/test").exchange()
responseSpec -> responseSpec.expectStatus().isBadRequest(), .expectAll(
responseSpec -> responseSpec.expectStatus().isOk(), responseSpec -> responseSpec.expectStatus().isBadRequest(),
responseSpec -> responseSpec.expectBody(String.class).isEqualTo("bogus") responseSpec -> responseSpec.expectStatus().isOk(),
responseSpec -> responseSpec.expectBody(String.class).isEqualTo("bogus")
)
) )
).withMessage("Multiple Exceptions (2):\nStatus expected:<400 BAD_REQUEST> but was:<200 OK>\nResponse body expected:<bogus> but was:<hello>"); .withMessage("Multiple Exceptions (2):\n" +
"Status expected:<400 BAD_REQUEST> but was:<200 OK>\n" +
"Response body expected:<bogus> but was:<hello>");
} }