made tests assertions non-blocking

This commit is contained in:
Gerardo Roza 2021-01-25 12:26:37 -03:00
parent a37812ce04
commit 615322468e
2 changed files with 45 additions and 34 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>spring-5-reactive</artifactId> <artifactId>spring-5-reactive</artifactId>
@ -73,6 +74,12 @@
<artifactId>spring-security-test</artifactId> <artifactId>spring-security-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring WebFlux WebSession --> <!-- Spring WebFlux WebSession -->
<dependency> <dependency>

View File

@ -46,6 +46,7 @@ import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient; import reactor.netty.http.client.HttpClient;
import reactor.test.StepVerifier;
@SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @SpringBootTest(classes = WebClientApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class WebClientIntegrationTest { public class WebClientIntegrationTest {
@ -107,44 +108,46 @@ public class WebClientIntegrationTest {
// request // request
ResponseSpec responseSpecPostString = headerSpecInserterStringWithHeaders.retrieve(); ResponseSpec responseSpecPostString = headerSpecInserterStringWithHeaders.retrieve();
String responsePostString = responseSpecPostString.bodyToMono(String.class) Mono<String> responsePostString = responseSpecPostString.bodyToMono(String.class);
.block(); Mono<String> responsePostMultipart = headerSpecInserterMultipart.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
String responsePostMultipart = headerSpecInserterMultipart.header(HttpHeaders.CONTENT_TYPE, MediaType.MULTIPART_FORM_DATA_VALUE)
.retrieve() .retrieve()
.bodyToMono(String.class) .bodyToMono(String.class);
.block(); Mono<String> responsePostWithBody1 = headerSpecPost1.retrieve()
String responsePostWithBody1 = headerSpecPost1.retrieve() .bodyToMono(String.class);
.bodyToMono(String.class) Mono<String> responsePostWithBody2 = headerSpecPost2.retrieve()
.block(); .bodyToMono(String.class);
String responsePostWithBody2 = headerSpecPost2.retrieve() Mono<String> responsePostWithBody3 = headerSpecPost2.retrieve()
.bodyToMono(String.class) .bodyToMono(String.class);
.block(); Mono<String> responsePostFoo = headerSpecFooPost.retrieve()
String responsePostWithBody3 = headerSpecPost2.retrieve() .bodyToMono(String.class);
.bodyToMono(String.class)
.block();
String responsePostFoo = headerSpecFooPost.retrieve()
.bodyToMono(String.class)
.block();
ParameterizedTypeReference<Map<String, String>> ref = new ParameterizedTypeReference<Map<String, String>>() { ParameterizedTypeReference<Map<String, String>> ref = new ParameterizedTypeReference<Map<String, String>>() {
}; };
Map<String, String> responseGet = headerSpecGet.retrieve() Mono<Map<String, String>> responseGet = headerSpecGet.retrieve()
.bodyToMono(ref) .bodyToMono(ref);
.block(); Mono<Map<String, String>> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> {
Map<String, String> responsePostWithNoBody = createDefaultPostResourceRequest().exchangeToMono(responseHandler -> {
assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST); assertThat(responseHandler.statusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
return responseHandler.bodyToMono(ref); return responseHandler.bodyToMono(ref);
}) });
.block();
// response assertions // response assertions
assertThat(responsePostString).isEqualTo("processed-bodyValue"); StepVerifier.create(responsePostString)
assertThat(responsePostMultipart).isEqualTo("processed-multipartValue1-multipartValue2"); .expectNext("processed-bodyValue");
assertThat(responsePostWithBody1).isEqualTo("processed-bodyValue"); StepVerifier.create(responsePostMultipart)
assertThat(responsePostWithBody2).isEqualTo("processed-bodyValue"); .expectNext("processed-multipartValue1-multipartValue2");
assertThat(responsePostWithBody3).isEqualTo("processed-bodyValue"); StepVerifier.create(responsePostWithBody1)
assertThat(responseGet).containsEntry("field", "value"); .expectNext("processed-bodyValue");
assertThat(responsePostFoo).isEqualTo("processedFoo-fooName"); StepVerifier.create(responsePostWithBody2)
assertThat(responsePostWithNoBody).containsEntry("error", "Bad Request"); .expectNext("processed-bodyValue");
StepVerifier.create(responsePostWithBody3)
.expectNext("processed-bodyValue");
StepVerifier.create(responseGet)
.expectNextMatches(nextMap -> nextMap.get("field")
.equals("value"));
StepVerifier.create(responsePostFoo)
.expectNext("processedFoo-fooName");
StepVerifier.create(responsePostWithNoBody)
.expectNextMatches(nextMap -> nextMap.get("error")
.equals("Bad Request"));
// assert sending plain `new Object()` as request body // assert sending plain `new Object()` as request body
assertThrows(CodecException.class, () -> { assertThrows(CodecException.class, () -> {
@ -152,11 +155,12 @@ public class WebClientIntegrationTest {
.block(); .block();
}); });
// assert sending request overriding base uri // assert sending request overriding base uri
assertThrows(WebClientRequestException.class, () -> { Exception exception = assertThrows(WebClientRequestException.class, () -> {
bodySpecOverridenBaseUri.retrieve() bodySpecOverridenBaseUri.retrieve()
.bodyToMono(String.class) .bodyToMono(String.class)
.block(); .block();
}); });
assertThat(exception.getMessage()).contains("Connection refused");
} }