[JAVA-12608] Split test scenarios and add logging

This commit is contained in:
Haroon Khan 2022-07-17 10:31:59 +01:00
parent 18f919bf30
commit 958bbb5cea
1 changed files with 33 additions and 14 deletions

View File

@ -82,6 +82,16 @@ class WebClientIntegrationTest {
.verify(); .verify();
} }
@Test
void givenWebClientCreationWithoutUri_whenUsed_thenObtainExpectedResponse() {
WebClient client = WebClient.create();
StepVerifier.create(retrieveResponse(client))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused"))
.verify();
}
@Test @Test
void givenDifferentMethodSpecifications_whenUsed_thenObtainExpectedResponse() { void givenDifferentMethodSpecifications_whenUsed_thenObtainExpectedResponse() {
// request specification // request specification
@ -111,12 +121,6 @@ class WebClientIntegrationTest {
.build()); .build());
RequestBodySpec bodySpecusingURI = createDefaultPostRequest().uri( RequestBodySpec bodySpecusingURI = createDefaultPostRequest().uri(
URI.create("http://localhost:" + port + "/resource")); URI.create("http://localhost:" + port + "/resource"));
RequestBodySpec bodySpecOverridenBaseUri = createDefaultPostRequest().uri(URI.create("/resource"));
RequestBodySpec bodySpecOverridenBaseUri2 = WebClient.builder()
.baseUrl("http://localhost:" + port)
.build()
.post()
.uri(URI.create("/resource"));
// response assertions // response assertions
StepVerifier.create(retrieveResponse(bodySpecUsingString)) StepVerifier.create(retrieveResponse(bodySpecUsingString))
@ -128,12 +132,22 @@ class WebClientIntegrationTest {
StepVerifier.create(retrieveResponse(bodySpecusingURI)) StepVerifier.create(retrieveResponse(bodySpecusingURI))
.expectNext("processed-bodyValue") .expectNext("processed-bodyValue")
.verifyComplete(); .verifyComplete();
// assert sending request overriding base URI }
StepVerifier.create(retrieveResponse(bodySpecOverridenBaseUri))
@Test
void givenOverriddenUriSpecifications_whenUsed_thenObtainExpectedResponse() {
RequestBodySpec bodySpecOverriddenBaseUri = createDefaultPostRequest().uri(URI.create("/resource"));
StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused")) .contains("Connection refused"))
.verify(); .verify();
StepVerifier.create(retrieveResponse(bodySpecOverridenBaseUri2))
RequestBodySpec bodySpecOverriddenBaseUri2 = WebClient.builder()
.baseUrl("http://localhost:" + port)
.build()
.post()
.uri(URI.create("/resource"));
StepVerifier.create(retrieveResponse(bodySpecOverriddenBaseUri2))
.expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage() .expectErrorMatches(ex -> WebClientRequestException.class.isAssignableFrom(ex.getClass()) && ex.getMessage()
.contains("Connection refused")) .contains("Connection refused"))
.verify(); .verify();
@ -301,30 +315,35 @@ class WebClientIntegrationTest {
.uri("/resource") .uri("/resource")
.bodyValue(BODY_VALUE) .bodyValue(BODY_VALUE)
.retrieve() .retrieve()
.bodyToMono(String.class); .bodyToMono(String.class)
.log();
} }
private Mono<String> retrieveResponse(RequestBodyUriSpec spec) { private Mono<String> retrieveResponse(RequestBodyUriSpec spec) {
return spec.uri("/resource") return spec.uri("/resource")
.bodyValue(BODY_VALUE) .bodyValue(BODY_VALUE)
.retrieve() .retrieve()
.bodyToMono(String.class); .bodyToMono(String.class)
.log();
} }
private Mono<Map<String, String>> retrieveGetResponse(RequestHeadersUriSpec<?> spec) { private Mono<Map<String, String>> retrieveGetResponse(RequestHeadersUriSpec<?> spec) {
return spec.uri("/resource") return spec.uri("/resource")
.retrieve() .retrieve()
.bodyToMono(MAP_RESPONSE_REF); .bodyToMono(MAP_RESPONSE_REF)
.log();
} }
private Mono<String> retrieveResponse(RequestBodySpec spec) { private Mono<String> retrieveResponse(RequestBodySpec spec) {
return spec.bodyValue(BODY_VALUE) return spec.bodyValue(BODY_VALUE)
.retrieve() .retrieve()
.bodyToMono(String.class); .bodyToMono(String.class)
.log();
} }
private Mono<String> retrieveResponse(RequestHeadersSpec<?> spec) { private Mono<String> retrieveResponse(RequestHeadersSpec<?> spec) {
return spec.retrieve() return spec.retrieve()
.bodyToMono(String.class); .bodyToMono(String.class)
.log();
} }
} }