Browse Source

Ensure one time logging for request details

Closes gh-26969
pull/27365/head
Rossen Stoyanchev 4 years ago
parent
commit
1b3fd9edff
  1. 7
      spring-test/src/main/java/org/springframework/test/web/reactive/server/ExchangeResult.java
  2. 20
      spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/SoftAssertionTests.java

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

@ -79,6 +79,9 @@ public class ExchangeResult { @@ -79,6 +79,9 @@ public class ExchangeResult {
@Nullable
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
@ -121,6 +124,7 @@ public class ExchangeResult { @@ -121,6 +124,7 @@ public class ExchangeResult {
this.timeout = other.timeout;
this.uriTemplate = other.uriTemplate;
this.mockServerResult = other.mockServerResult;
this.diagnosticsLogged = other.diagnosticsLogged;
}
@ -227,7 +231,8 @@ public class ExchangeResult { @@ -227,7 +231,8 @@ public class ExchangeResult {
assertion.run();
}
catch (AssertionError ex) {
if (logger.isErrorEnabled()) {
if (!this.diagnosticsLogged && logger.isErrorEnabled()) {
this.diagnosticsLogged = true;
logger.error("Request details for assertion failure:\n" + this);
}
throw ex;

20
spring-test/src/test/java/org/springframework/test/web/reactive/server/samples/SoftAssertionTests.java

@ -46,15 +46,19 @@ class SoftAssertionTests { @@ -46,15 +46,19 @@ class SoftAssertionTests {
}
@Test
void expectAllWithMultipleFailures() throws Exception {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
this.webTestClient.get().uri("/test").exchange()
.expectAll(
responseSpec -> responseSpec.expectStatus().isBadRequest(),
responseSpec -> responseSpec.expectStatus().isOk(),
responseSpec -> responseSpec.expectBody(String.class).isEqualTo("bogus")
void expectAllWithMultipleFailures() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() ->
this.webTestClient.get().uri("/test").exchange()
.expectAll(
responseSpec -> responseSpec.expectStatus().isBadRequest(),
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>");
}

Loading…
Cancel
Save