From 5b9e7e44e027718447504f417fb3821c468415fa Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Wed, 11 Apr 2018 16:19:53 -0400 Subject: [PATCH] Improve WebClient test with ParameterizedTypeReference Issue: SPR-16715 --- .../client/WebClientIntegrationTests.java | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java index e575d871734..5b502c4aa52 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -48,9 +48,7 @@ import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.http.codec.Pojo; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; /** * Integration tests using a {@link ExchangeFunction} through {@link WebClient}. @@ -145,20 +143,23 @@ public class WebClientIntegrationTests { }); } - @Test + @Test // SPR-16715 public void shouldReceiveJsonAsTypeReferenceString() throws Exception { - String content = "{\"bar\":\"barbar\",\"foo\":\"foofoo\"}"; + String content = "{\"containerValue\":{\"fooValue\":\"bar\"}}"; prepareResponse(response -> response .setHeader("Content-Type", "application/json").setBody(content)); - Mono result = this.webClient.get() + Mono> result = this.webClient.get() .uri("/json").accept(MediaType.APPLICATION_JSON) .retrieve() - .bodyToMono(new ParameterizedTypeReference() { - }); + .bodyToMono(new ParameterizedTypeReference>() {}); StepVerifier.create(result) - .expectNext(content) + .assertNext(valueContainer -> { + Foo foo = valueContainer.getContainerValue(); + assertNotNull(foo); + assertEquals("bar", foo.getFooValue()); + }) .expectComplete().verify(Duration.ofSeconds(3)); expectRequestCount(1); @@ -651,4 +652,33 @@ public class WebClientIntegrationTests { } } + + static class ValueContainer { + + private T containerValue; + + + public T getContainerValue() { + return containerValue; + } + + public void setContainerValue(T containerValue) { + this.containerValue = containerValue; + } + } + + static class Foo { + + private String fooValue; + + + public String getFooValue() { + return fooValue; + } + + public void setFooValue(String fooValue) { + this.fooValue = fooValue; + } + } + }