From: Kai Moritz Date: Sat, 11 Jan 2020 15:48:54 +0000 (+0100) Subject: WIP X-Git-Tag: spec-stub-holzweg~1 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=8c39b69737931d6db70ccedee40b3cf12402fa0d;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 e458479..a8cecab 100644 --- a/src/test/java/de/juplo/demo/RemoteContentServiceTest.java +++ b/src/test/java/de/juplo/demo/RemoteContentServiceTest.java @@ -1,17 +1,33 @@ package de.juplo.demo; +import java.net.URI; +import java.nio.charset.Charset; +import java.time.ZonedDateTime; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Function; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; +import org.reactivestreams.Publisher; 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.MediaType; +import org.springframework.http.client.reactive.ClientHttpRequest; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.util.MultiValueMap; +import org.springframework.web.reactive.function.BodyInserter; +import org.springframework.web.reactive.function.client.ClientResponse; import org.springframework.web.reactive.function.client.WebClient; import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec; 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.Mono; import reactor.test.StepVerifier; @@ -57,4 +73,339 @@ public class RemoteContentServiceTest .expectNext("bar") .verifyComplete(); } + + + 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(); + + + private WebClientStub() {} + + + @Override + public RequestHeadersUriSpec get() + { + return URI_SPEC; + } + + @Override + public RequestHeadersUriSpec head() + { + return URI_SPEC; + } + + @Override + public RequestBodyUriSpec post() + { + return BODY_SPEC; + } + + @Override + public RequestBodyUriSpec put() + { + return BODY_SPEC; + } + + @Override + public RequestBodyUriSpec patch() + { + return BODY_SPEC; + } + + @Override + public RequestHeadersUriSpec delete() + { + return URI_SPEC; + } + + @Override + public RequestHeadersUriSpec options() + { + return URI_SPEC; + } + + @Override + public RequestBodyUriSpec method(HttpMethod hm) + { + return BODY_SPEC; + } + + @Override + public Builder mutate() + { + throw new UnsupportedOperationException("Stub is immutable!"); + } + + + static class RequestHeadersUriSpecStub implements RequestHeadersUriSpec + { + private RequestHeadersUriSpecStub() {} + + + @Override + public RequestHeadersSpec uri(URI uri) + { + return this; + } + + @Override + public RequestHeadersSpec uri(String string, Object... os) + { + return this; + } + + @Override + public RequestHeadersSpec uri(String string, Map map) + { + return this; + } + + @Override + public RequestHeadersSpec uri(String string, Function fnctn) + { + return this; + } + + @Override + public RequestHeadersSpec uri(Function fnctn) + { + return this; + } + + @Override + public RequestHeadersSpec accept(MediaType... mts) + { + return this; + } + + @Override + public RequestHeadersSpec acceptCharset(Charset... chrsts) + { + return this; + } + + @Override + public RequestHeadersSpec cookie(String string, String string1) + { + return this; + } + + @Override + public RequestHeadersSpec cookies(Consumer cnsmr) + { + return this; + } + + @Override + public RequestHeadersSpec ifModifiedSince(ZonedDateTime zdt) + { + return this; + } + + @Override + public RequestHeadersSpec ifNoneMatch(String... strings) + { + return this; + } + + @Override + public RequestHeadersSpec header(String string, String... strings) + { + return this; + } + + @Override + public RequestHeadersSpec headers(Consumer cnsmr) + { + return this; + } + + @Override + public RequestHeadersSpec attribute(String string, Object o) + { + return this; + } + + @Override + public RequestHeadersSpec attributes(Consumer cnsmr) + { + return this; + } + + @Override + public ResponseSpec retrieve() + { + } + + @Override + public Mono exchange() + { + } + } + + static class RequestBodySpecStub implements RequestBodyUriSpec + { + private RequestBodySpecStub() {} + + + @Override + public RequestBodySpec contentLength(long l) + { + return this; + } + + @Override + public RequestBodySpec contentType(MediaType mt) + { + return this; + } + + @Override + public RequestHeadersSpec bodyValue(Object o) + { + return this; + } + + @Override + public > RequestHeadersSpec body(P p, Class type) + { + return this; + } + + @Override + public > RequestHeadersSpec body(P p, ParameterizedTypeReference ptr) + { + return this; + } + + @Override + public RequestHeadersSpec body(Object o, Class type) + { + return this; + } + + @Override + public RequestHeadersSpec body(Object o, ParameterizedTypeReference ptr) + { + return this; + } + + @Override + public RequestHeadersSpec body( BodyInserter bi) + { + return this; + } + + @Override + public RequestHeadersSpec syncBody(Object o) + { + return this; + } + + @Override + public RequestBodySpec accept(MediaType... mts) + { + return this; + } + + @Override + public RequestBodySpec acceptCharset(Charset... chrsts) + { + return this; + } + + @Override + public RequestBodySpec cookie(String string, String string1) + { + return this; + } + + @Override + public RequestBodySpec cookies(Consumer> cnsmr) + { + return this; + } + + @Override + public RequestBodySpec ifModifiedSince(ZonedDateTime zdt) + { + return this; + } + + @Override + public RequestBodySpec ifNoneMatch(String... strings) + { + return this; + } + + @Override + public RequestBodySpec header(String string, String... strings) + { + return this; + } + + @Override + public RequestBodySpec headers(Consumer cnsmr) + { + return this; + } + + @Override + public RequestBodySpec attribute(String string, Object o) + { + return this; + } + + @Override + public RequestBodySpec attributes(Consumer> cnsmr) + { + return this; + } + + @Override + public ResponseSpec retrieve() + { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Mono exchange() + { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public RequestBodySpec uri(URI uri) + { + return this; + } + + @Override + public RequestBodySpec uri(String string, Object... os) + { + return this; + } + + @Override + public RequestBodySpec uri(String string, Map map) + { + return this; + } + + @Override + public RequestBodySpec uri(String string, Function fnctn) + { + return this; + } + + @Override + public RequestBodySpec uri(Function fnctn) + { + return this; + } + } + } }