Browse Source

WebTestClient supports customer argument resolvers

Issue: SPR-15495
pull/1409/head
Rossen Stoyanchev 8 years ago
parent
commit
1292bb20f9
  1. 16
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java
  2. 9
      spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java
  3. 56
      spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultControllerSpecTests.java

16
spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultControllerSpec.java

@ -35,6 +35,7 @@ import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration; @@ -35,6 +35,7 @@ import org.springframework.web.reactive.config.DelegatingWebFluxConfiguration;
import org.springframework.web.reactive.config.PathMatchConfigurer;
import org.springframework.web.reactive.config.ViewResolverRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
import org.springframework.web.server.adapter.WebHttpHandlerBuilder;
/**
@ -77,6 +78,12 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control @@ -77,6 +78,12 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
return this;
}
@Override
public DefaultControllerSpec argumentResolvers(Consumer<ArgumentResolverConfigurer> consumer) {
this.configurer.argumentResolverConsumer = consumer;
return this;
}
@Override
public DefaultControllerSpec pathMatching(Consumer<PathMatchConfigurer> consumer) {
this.configurer.pathMatchConsumer = consumer;
@ -136,6 +143,8 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control @@ -136,6 +143,8 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
private Consumer<CorsRegistry> corsRegistryConsumer;
private Consumer<ArgumentResolverConfigurer> argumentResolverConsumer;
private Consumer<PathMatchConfigurer> pathMatchConsumer;
private Consumer<ServerCodecConfigurer> messageCodecsConsumer;
@ -168,6 +177,13 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control @@ -168,6 +177,13 @@ class DefaultControllerSpec extends AbstractMockServerSpec<WebTestClient.Control
}
}
@Override
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
if (this.argumentResolverConsumer != null) {
this.argumentResolverConsumer.accept(configurer);
}
}
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
if (this.messageCodecsConsumer != null) {

9
spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

@ -48,6 +48,7 @@ import org.springframework.web.reactive.function.client.ExchangeFunction; @@ -48,6 +48,7 @@ import org.springframework.web.reactive.function.client.ExchangeFunction;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.util.UriBuilder;
@ -243,7 +244,13 @@ public interface WebTestClient { @@ -243,7 +244,13 @@ public interface WebTestClient {
ControllerSpec pathMatching(Consumer<PathMatchConfigurer> consumer);
/**
* Modify or extend the list of built-in message readers and writers.
* Configure resolvers for custom controller method arguments.
* @see WebFluxConfigurer#configureHttpMessageCodecs
*/
ControllerSpec argumentResolvers(Consumer<ArgumentResolverConfigurer> configurer);
/**
* Configure custom HTTP message readers and writers or override built-in ones.
* @see WebFluxConfigurer#configureHttpMessageCodecs
*/
ControllerSpec httpMessageCodecs(Consumer<ServerCodecConfigurer> configurer);

56
spring-test/src/test/java/org/springframework/test/web/reactive/server/DefaultControllerSpecTests.java

@ -16,13 +16,24 @@ @@ -16,13 +16,24 @@
package org.springframework.test.web.reactive.server;
import java.util.function.Consumer;
import org.junit.Test;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.ResponseEntity;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.accept.RequestedContentTypeResolverBuilder;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.reactive.config.PathMatchConfigurer;
import org.springframework.web.reactive.config.ViewResolverRegistry;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
import static org.junit.Assert.assertNotNull;
/**
* Unit tests for {@link DefaultControllerSpec}.
@ -52,6 +63,36 @@ public class DefaultControllerSpecTests { @@ -52,6 +63,36 @@ public class DefaultControllerSpecTests {
.expectBody(String.class).isEqualTo("Handled exception");
}
@Test
public void configurerConsumers() throws Exception {
TestConsumer<ArgumentResolverConfigurer> argumentResolverConsumer = new TestConsumer<>();
TestConsumer<RequestedContentTypeResolverBuilder> contenTypeResolverConsumer = new TestConsumer<>();
TestConsumer<CorsRegistry> corsRegistryConsumer = new TestConsumer<>();
TestConsumer<FormatterRegistry> formatterConsumer = new TestConsumer<>();
TestConsumer<ServerCodecConfigurer> codecsConsumer = new TestConsumer<>();
TestConsumer<PathMatchConfigurer> pathMatchingConsumer = new TestConsumer<>();
TestConsumer<ViewResolverRegistry> viewResolverConsumer = new TestConsumer<>();
new DefaultControllerSpec(new MyController())
.argumentResolvers(argumentResolverConsumer)
.contentTypeResolver(contenTypeResolverConsumer)
.corsMappings(corsRegistryConsumer)
.formatters(formatterConsumer)
.httpMessageCodecs(codecsConsumer)
.pathMatching(pathMatchingConsumer)
.viewResolvers(viewResolverConsumer)
.build();
assertNotNull(argumentResolverConsumer.getValue());
assertNotNull(contenTypeResolverConsumer.getValue());
assertNotNull(corsRegistryConsumer.getValue());
assertNotNull(formatterConsumer.getValue());
assertNotNull(codecsConsumer.getValue());
assertNotNull(pathMatchingConsumer.getValue());
assertNotNull(viewResolverConsumer.getValue());
}
@RestController
private static class MyController {
@ -77,4 +118,19 @@ public class DefaultControllerSpecTests { @@ -77,4 +118,19 @@ public class DefaultControllerSpecTests {
}
}
private static class TestConsumer<T> implements Consumer<T> {
private T value;
public T getValue() {
return this.value;
}
@Override
public void accept(T t) {
this.value = t;
}
}
}

Loading…
Cancel
Save