From: Kai Moritz Date: Sat, 11 Jan 2020 16:44:51 +0000 (+0100) Subject: WIP X-Git-Tag: spec-stub-holzweg X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=HEAD;p=demos%2Ftesting WIP --- diff --git a/src/test/java/de/juplo/demo/RemoteContentServiceTest.java b/src/test/java/de/juplo/demo/RemoteContentServiceTest.java index a8cecab..e5942d6 100644 --- a/src/test/java/de/juplo/demo/RemoteContentServiceTest.java +++ b/src/test/java/de/juplo/demo/RemoteContentServiceTest.java @@ -4,9 +4,13 @@ package de.juplo.demo; import java.net.URI; import java.nio.charset.Charset; import java.time.ZonedDateTime; +import java.util.List; import java.util.Map; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.IntPredicate; +import java.util.function.Predicate; +import java.util.function.Supplier; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -17,7 +21,9 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.util.MultiValueMap; @@ -28,6 +34,7 @@ import org.springframework.web.reactive.function.client.WebClient.RequestHeaders import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec; import org.springframework.web.reactive.function.client.WebClient.ResponseSpec; import org.springframework.web.util.UriBuilder; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; import reactor.test.StepVerifier; @@ -78,8 +85,12 @@ public class RemoteContentServiceTest static class WebClientStub implements WebClient { final static WebClientStub WEB_CLIENT = new WebClientStub(); - final static RequestHeadersUriSpecStub URI_SPEC = new RequestHeadersUriSpecStub(); - final static RequestBodySpecStub BODY_SPEC = new RequestBodySpecStub(); + final static RequestHeadersUriSpecStub HEADERS_SPEC = new RequestHeadersUriSpecStub(); + final static RequestBodySpecStub REQUEST_BODY_SPEC = new RequestBodySpecStub(); + final static ResponseSpecStub RESPONSE_SPEC = new ResponseSpecStub(); + + static Supplier> toMono; + static Supplier> toFlux; private WebClientStub() {} @@ -88,49 +99,49 @@ public class RemoteContentServiceTest @Override public RequestHeadersUriSpec get() { - return URI_SPEC; + return HEADERS_SPEC; } @Override public RequestHeadersUriSpec head() { - return URI_SPEC; + return HEADERS_SPEC; } @Override public RequestBodyUriSpec post() { - return BODY_SPEC; + return REQUEST_BODY_SPEC; } @Override public RequestBodyUriSpec put() { - return BODY_SPEC; + return REQUEST_BODY_SPEC; } @Override public RequestBodyUriSpec patch() { - return BODY_SPEC; + return REQUEST_BODY_SPEC; } @Override public RequestHeadersUriSpec delete() { - return URI_SPEC; + return HEADERS_SPEC; } @Override public RequestHeadersUriSpec options() { - return URI_SPEC; + return HEADERS_SPEC; } @Override public RequestBodyUriSpec method(HttpMethod hm) { - return BODY_SPEC; + return REQUEST_BODY_SPEC; } @Override @@ -238,11 +249,13 @@ public class RemoteContentServiceTest @Override public ResponseSpec retrieve() { + return RESPONSE_SPEC; } @Override public Mono exchange() { + return WebClientStub.toMono.get(); } } @@ -407,5 +420,77 @@ public class RemoteContentServiceTest return this; } } + + static class ResponseSpecStub implements ResponseSpec + { + private ResponseSpecStub() {} + + + @Override + public ResponseSpec onStatus(Predicate statusPredicate, Function> exceptionFunction) + { + return this; + } + + @Override + public ResponseSpec onRawStatus(IntPredicate statusCodePredicate, Function> exceptionFunction) + { + return this; + } + + @Override + public Mono bodyToMono(Class elementClass) + { + return (Mono)toMono.get(); + } + + @Override + public Mono bodyToMono(ParameterizedTypeReference elementTypeRef) + { + return (Mono)toMono.get(); + } + + @Override + public Flux bodyToFlux(Class elementClass) + { + return (Flux)toFlux; + } + + @Override + public Flux bodyToFlux(ParameterizedTypeReference elementTypeRef) + { + return (Flux)toFlux; + } + + @Override + public Mono> toEntity(Class bodyClass) + { + return (Mono>)toMono.get(); + } + + @Override + public Mono> toEntity(ParameterizedTypeReference bodyTypeReference) + { + return (Mono>)toMono.get(); + } + + @Override + public Mono>> toEntityList(Class elementClass) + { + return (Mono>>)toMono.get(); + } + + @Override + public Mono>> toEntityList(ParameterizedTypeReference elementTypeRef) + { + return (Mono>>)toMono.get(); + } + + @Override + public Mono> toBodilessEntity() + { + return (Mono>)toMono.get(); + } + } } }