From 336a5d0ac848853220a14c39101c4e4ef44e67b8 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Fri, 18 Jul 2025 06:46:31 +0100 Subject: [PATCH] Add container for MockMvcServerServerSpec hierarchy See gh-34428 --- .../client/AbstractMockMvcServerSpec.java | 107 ----- .../client/ApplicationContextMockMvcSpec.java | 44 -- .../servlet/client/MockMvcWebTestClient.java | 6 +- .../client/MockMvcWebTestClientSpecs.java | 378 ++++++++++++++++++ .../client/RouterFunctionMockMvcSpec.java | 101 ----- .../servlet/client/StandaloneMockMvcSpec.java | 179 --------- 6 files changed, 381 insertions(+), 434 deletions(-) delete mode 100644 spring-test/src/main/java/org/springframework/test/web/servlet/client/AbstractMockMvcServerSpec.java delete mode 100644 spring-test/src/main/java/org/springframework/test/web/servlet/client/ApplicationContextMockMvcSpec.java create mode 100644 spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcWebTestClientSpecs.java delete mode 100644 spring-test/src/main/java/org/springframework/test/web/servlet/client/RouterFunctionMockMvcSpec.java delete mode 100644 spring-test/src/main/java/org/springframework/test/web/servlet/client/StandaloneMockMvcSpec.java diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/client/AbstractMockMvcServerSpec.java b/spring-test/src/main/java/org/springframework/test/web/servlet/client/AbstractMockMvcServerSpec.java deleted file mode 100644 index 6db5a2f3894..00000000000 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/client/AbstractMockMvcServerSpec.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Copyright 2002-present 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.test.web.servlet.client; - -import jakarta.servlet.Filter; - -import org.springframework.http.client.reactive.ClientHttpConnector; -import org.springframework.test.web.reactive.server.WebTestClient; -import org.springframework.test.web.servlet.DispatcherServletCustomizer; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.RequestBuilder; -import org.springframework.test.web.servlet.ResultMatcher; -import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; -import org.springframework.test.web.servlet.setup.MockMvcConfigurer; - -/** - * Base class for implementations of {@link MockMvcWebTestClient.MockMvcServerSpec} - * that simply delegates to a {@link ConfigurableMockMvcBuilder} supplied by - * the concrete subclasses. - * - * @author Rossen Stoyanchev - * @since 5.3 - * @param the type of the concrete subclass spec - */ -abstract class AbstractMockMvcServerSpec> - implements MockMvcWebTestClient.MockMvcServerSpec { - - @Override - public T filters(Filter... filters) { - getMockMvcBuilder().addFilters(filters); - return self(); - } - - @Override - public final T filter(Filter filter, String... urlPatterns) { - getMockMvcBuilder().addFilter(filter, urlPatterns); - return self(); - } - - @Override - public T defaultRequest(RequestBuilder requestBuilder) { - getMockMvcBuilder().defaultRequest(requestBuilder); - return self(); - } - - @Override - public T alwaysExpect(ResultMatcher resultMatcher) { - getMockMvcBuilder().alwaysExpect(resultMatcher); - return self(); - } - - @Override - public T dispatchOptions(boolean dispatchOptions) { - getMockMvcBuilder().dispatchOptions(dispatchOptions); - return self(); - } - - @Override - public T dispatcherServletCustomizer(DispatcherServletCustomizer customizer) { - getMockMvcBuilder().addDispatcherServletCustomizer(customizer); - return self(); - } - - @Override - public T apply(MockMvcConfigurer configurer) { - getMockMvcBuilder().apply(configurer); - return self(); - } - - @SuppressWarnings("unchecked") - private T self() { - return (T) this; - } - - /** - * Return the concrete {@link ConfigurableMockMvcBuilder} to delegate - * configuration methods and to use to create the {@link MockMvc}. - */ - protected abstract ConfigurableMockMvcBuilder getMockMvcBuilder(); - - @Override - public WebTestClient.Builder configureClient() { - MockMvc mockMvc = getMockMvcBuilder().build(); - ClientHttpConnector connector = new MockMvcHttpConnector(mockMvc); - return WebTestClient.bindToServer(connector); - } - - @Override - public WebTestClient build() { - return configureClient().build(); - } - -} diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/client/ApplicationContextMockMvcSpec.java b/spring-test/src/main/java/org/springframework/test/web/servlet/client/ApplicationContextMockMvcSpec.java deleted file mode 100644 index 0aaca79d937..00000000000 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/client/ApplicationContextMockMvcSpec.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 2002-present 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.test.web.servlet.client; - -import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; -import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -/** - * Simple wrapper around a {@link DefaultMockMvcBuilder}. - * - * @author Rossen Stoyanchev - * @since 5.3 - */ -class ApplicationContextMockMvcSpec extends AbstractMockMvcServerSpec { - - private final DefaultMockMvcBuilder mockMvcBuilder; - - - public ApplicationContextMockMvcSpec(WebApplicationContext context) { - this.mockMvcBuilder = MockMvcBuilders.webAppContextSetup(context); - } - - @Override - protected ConfigurableMockMvcBuilder getMockMvcBuilder() { - return this.mockMvcBuilder; - } - -} diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcWebTestClient.java b/spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcWebTestClient.java index 47a6692dc96..8f69dac8736 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcWebTestClient.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcWebTestClient.java @@ -88,7 +88,7 @@ public interface MockMvcWebTestClient { * to initialize {@link MockMvc}. */ static ControllerSpec bindToController(Object... controllers) { - return new StandaloneMockMvcSpec(controllers); + return new MockMvcWebTestClientSpecs.StandaloneMockMvcSpec(controllers); } /** @@ -100,7 +100,7 @@ public interface MockMvcWebTestClient { * @since 6.2 */ static RouterFunctionSpec bindToRouterFunction(RouterFunction... routerFunctions) { - return new RouterFunctionMockMvcSpec(routerFunctions); + return new MockMvcWebTestClientSpecs.RouterFunctionMockMvcSpec(routerFunctions); } /** @@ -112,7 +112,7 @@ public interface MockMvcWebTestClient { * to initialize {@code MockMvc}. */ static MockMvcServerSpec bindToApplicationContext(WebApplicationContext context) { - return new ApplicationContextMockMvcSpec(context); + return new MockMvcWebTestClientSpecs.ApplicationContextMockMvcSpec(context); } /** diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcWebTestClientSpecs.java b/spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcWebTestClientSpecs.java new file mode 100644 index 00000000000..6b59f956dff --- /dev/null +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/client/MockMvcWebTestClientSpecs.java @@ -0,0 +1,378 @@ +/* + * Copyright 2002-present 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.test.web.servlet.client; + + +import java.util.function.Supplier; + +import jakarta.servlet.Filter; +import org.jspecify.annotations.Nullable; + +import org.springframework.format.support.FormattingConversionService; +import org.springframework.http.client.reactive.ClientHttpConnector; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.test.web.servlet.DispatcherServletCustomizer; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; +import org.springframework.test.web.servlet.ResultMatcher; +import org.springframework.test.web.servlet.client.MockMvcWebTestClient.MockMvcServerSpec; +import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; +import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.test.web.servlet.setup.MockMvcConfigurer; +import org.springframework.test.web.servlet.setup.RouterFunctionMockMvcBuilder; +import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder; +import org.springframework.validation.Validator; +import org.springframework.web.accept.ApiVersionStrategy; +import org.springframework.web.accept.ContentNegotiationManager; +import org.springframework.web.context.WebApplicationContext; +import org.springframework.web.method.support.HandlerMethodArgumentResolver; +import org.springframework.web.method.support.HandlerMethodReturnValueHandler; +import org.springframework.web.servlet.FlashMapManager; +import org.springframework.web.servlet.HandlerExceptionResolver; +import org.springframework.web.servlet.HandlerInterceptor; +import org.springframework.web.servlet.LocaleResolver; +import org.springframework.web.servlet.View; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.function.RouterFunction; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; +import org.springframework.web.util.pattern.PathPatternParser; + +/** + * Container class to encapsulate the {@link MockMvcServerSpec} implementation + * hierarchy. This class was added in 7.0 to reduce mixing WebTestClient and + * RestTestClient classes in the same package. + * + * @author Rossen Stoyanchev + * @since 7.0 + */ +abstract class MockMvcWebTestClientSpecs { + + /** + * Base class for implementations of {@link MockMvcServerSpec} + * that simply delegates to a {@link ConfigurableMockMvcBuilder} supplied by + * the concrete subclasses. + * + * @author Rossen Stoyanchev + * @since 5.3 + * @param the type of the concrete subclass spec + */ + abstract static class AbstractMockMvcServerSpec> + implements MockMvcServerSpec { + + @Override + public T filters(Filter... filters) { + getMockMvcBuilder().addFilters(filters); + return self(); + } + + @Override + public final T filter(Filter filter, String... urlPatterns) { + getMockMvcBuilder().addFilter(filter, urlPatterns); + return self(); + } + + @Override + public T defaultRequest(RequestBuilder requestBuilder) { + getMockMvcBuilder().defaultRequest(requestBuilder); + return self(); + } + + @Override + public T alwaysExpect(ResultMatcher resultMatcher) { + getMockMvcBuilder().alwaysExpect(resultMatcher); + return self(); + } + + @Override + public T dispatchOptions(boolean dispatchOptions) { + getMockMvcBuilder().dispatchOptions(dispatchOptions); + return self(); + } + + @Override + public T dispatcherServletCustomizer(DispatcherServletCustomizer customizer) { + getMockMvcBuilder().addDispatcherServletCustomizer(customizer); + return self(); + } + + @Override + public T apply(MockMvcConfigurer configurer) { + getMockMvcBuilder().apply(configurer); + return self(); + } + + @SuppressWarnings("unchecked") + private T self() { + return (T) this; + } + + /** + * Return the concrete {@link ConfigurableMockMvcBuilder} to delegate + * configuration methods and to use to create the {@link MockMvc}. + */ + protected abstract ConfigurableMockMvcBuilder getMockMvcBuilder(); + + @Override + public WebTestClient.Builder configureClient() { + MockMvc mockMvc = getMockMvcBuilder().build(); + ClientHttpConnector connector = new MockMvcHttpConnector(mockMvc); + return WebTestClient.bindToServer(connector); + } + + @Override + public WebTestClient build() { + return configureClient().build(); + } + + } + + + /** + * Simple wrapper around a {@link DefaultMockMvcBuilder}. + * + * @author Rossen Stoyanchev + * @since 5.3 + */ + static class ApplicationContextMockMvcSpec extends AbstractMockMvcServerSpec { + + private final DefaultMockMvcBuilder mockMvcBuilder; + + + public ApplicationContextMockMvcSpec(WebApplicationContext context) { + this.mockMvcBuilder = MockMvcBuilders.webAppContextSetup(context); + } + + @Override + protected ConfigurableMockMvcBuilder getMockMvcBuilder() { + return this.mockMvcBuilder; + } + + } + + + /** + * Simple wrapper around a {@link RouterFunctionMockMvcBuilder} that implements + * {@link MockMvcWebTestClient.RouterFunctionSpec}. + * + * @author Arjen Poutsma + * @since 6.2 + */ + static class RouterFunctionMockMvcSpec extends AbstractMockMvcServerSpec + implements MockMvcWebTestClient.RouterFunctionSpec { + + private final RouterFunctionMockMvcBuilder mockMvcBuilder; + + + RouterFunctionMockMvcSpec(RouterFunction... routerFunctions) { + this.mockMvcBuilder = MockMvcBuilders.routerFunctions(routerFunctions); + } + + @Override + public MockMvcWebTestClient.RouterFunctionSpec messageConverters(HttpMessageConverter... messageConverters) { + this.mockMvcBuilder.setMessageConverters(messageConverters); + return this; + } + + @Override + public MockMvcWebTestClient.RouterFunctionSpec interceptors(HandlerInterceptor... interceptors) { + mappedInterceptors(null, interceptors); + return this; + } + + @Override + public MockMvcWebTestClient.RouterFunctionSpec mappedInterceptors(String @Nullable [] pathPatterns, HandlerInterceptor... interceptors) { + this.mockMvcBuilder.addMappedInterceptors(pathPatterns, interceptors); + return this; + } + + @Override + public MockMvcWebTestClient.RouterFunctionSpec asyncRequestTimeout(long timeout) { + this.mockMvcBuilder.setAsyncRequestTimeout(timeout); + return this; + } + + @Override + public MockMvcWebTestClient.RouterFunctionSpec handlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers) { + this.mockMvcBuilder.setHandlerExceptionResolvers(exceptionResolvers); + return this; + } + + @Override + public MockMvcWebTestClient.RouterFunctionSpec viewResolvers(ViewResolver... resolvers) { + this.mockMvcBuilder.setViewResolvers(resolvers); + return this; + } + + @Override + public MockMvcWebTestClient.RouterFunctionSpec singleView(View view) { + this.mockMvcBuilder.setSingleView(view); + return this; + } + + @Override + public MockMvcWebTestClient.RouterFunctionSpec patternParser(PathPatternParser parser) { + this.mockMvcBuilder.setPatternParser(parser); + return this; + } + + @Override + protected ConfigurableMockMvcBuilder getMockMvcBuilder() { + return this.mockMvcBuilder; + } + } + + /** + * Simple wrapper around a {@link StandaloneMockMvcBuilder} that implements + * {@link MockMvcWebTestClient.ControllerSpec}. + * + * @author Rossen Stoyanchev + * @since 5.3 + */ + static class StandaloneMockMvcSpec extends AbstractMockMvcServerSpec + implements MockMvcWebTestClient.ControllerSpec { + + private final StandaloneMockMvcBuilder mockMvcBuilder; + + + StandaloneMockMvcSpec(Object... controllers) { + this.mockMvcBuilder = MockMvcBuilders.standaloneSetup(controllers); + } + + @Override + public StandaloneMockMvcSpec controllerAdvice(Object... controllerAdvice) { + this.mockMvcBuilder.setControllerAdvice(controllerAdvice); + return this; + } + + @Override + public StandaloneMockMvcSpec messageConverters(HttpMessageConverter... messageConverters) { + this.mockMvcBuilder.setMessageConverters(messageConverters); + return this; + } + + @Override + public StandaloneMockMvcSpec validator(Validator validator) { + this.mockMvcBuilder.setValidator(validator); + return this; + } + + @Override + public StandaloneMockMvcSpec conversionService(FormattingConversionService conversionService) { + this.mockMvcBuilder.setConversionService(conversionService); + return this; + } + + @Override + public MockMvcWebTestClient.ControllerSpec apiVersionStrategy(ApiVersionStrategy versionStrategy) { + this.mockMvcBuilder.setApiVersionStrategy(versionStrategy); + return this; + } + + @Override + public StandaloneMockMvcSpec interceptors(HandlerInterceptor... interceptors) { + mappedInterceptors(null, interceptors); + return this; + } + + @Override + public StandaloneMockMvcSpec mappedInterceptors( + String @Nullable [] pathPatterns, HandlerInterceptor... interceptors) { + + this.mockMvcBuilder.addMappedInterceptors(pathPatterns, interceptors); + return this; + } + + @Override + public StandaloneMockMvcSpec contentNegotiationManager(ContentNegotiationManager manager) { + this.mockMvcBuilder.setContentNegotiationManager(manager); + return this; + } + + @Override + public StandaloneMockMvcSpec asyncRequestTimeout(long timeout) { + this.mockMvcBuilder.setAsyncRequestTimeout(timeout); + return this; + } + + @Override + public StandaloneMockMvcSpec customArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers) { + this.mockMvcBuilder.setCustomArgumentResolvers(argumentResolvers); + return this; + } + + @Override + public StandaloneMockMvcSpec customReturnValueHandlers(HandlerMethodReturnValueHandler... handlers) { + this.mockMvcBuilder.setCustomReturnValueHandlers(handlers); + return this; + } + + @Override + public StandaloneMockMvcSpec handlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers) { + this.mockMvcBuilder.setHandlerExceptionResolvers(exceptionResolvers); + return this; + } + + @Override + public StandaloneMockMvcSpec viewResolvers(ViewResolver... resolvers) { + this.mockMvcBuilder.setViewResolvers(resolvers); + return this; + } + + @Override + public StandaloneMockMvcSpec singleView(View view) { + this.mockMvcBuilder.setSingleView(view); + return this; + } + + @Override + public StandaloneMockMvcSpec localeResolver(LocaleResolver localeResolver) { + this.mockMvcBuilder.setLocaleResolver(localeResolver); + return this; + } + + @Override + public StandaloneMockMvcSpec flashMapManager(FlashMapManager flashMapManager) { + this.mockMvcBuilder.setFlashMapManager(flashMapManager); + return this; + } + + @Override + public StandaloneMockMvcSpec patternParser(PathPatternParser parser) { + this.mockMvcBuilder.setPatternParser(parser); + return this; + } + + @Override + public StandaloneMockMvcSpec placeholderValue(String name, String value) { + this.mockMvcBuilder.addPlaceholderValue(name, value); + return this; + } + + @Override + public StandaloneMockMvcSpec customHandlerMapping(Supplier factory) { + this.mockMvcBuilder.setCustomHandlerMapping(factory); + return this; + } + + @Override + public ConfigurableMockMvcBuilder getMockMvcBuilder() { + return this.mockMvcBuilder; + } + } +} diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/client/RouterFunctionMockMvcSpec.java b/spring-test/src/main/java/org/springframework/test/web/servlet/client/RouterFunctionMockMvcSpec.java deleted file mode 100644 index 8cccb127fff..00000000000 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/client/RouterFunctionMockMvcSpec.java +++ /dev/null @@ -1,101 +0,0 @@ -/* - * Copyright 2002-present 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.test.web.servlet.client; - -import org.jspecify.annotations.Nullable; - -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.test.web.servlet.setup.RouterFunctionMockMvcBuilder; -import org.springframework.web.servlet.HandlerExceptionResolver; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.View; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.function.RouterFunction; -import org.springframework.web.util.pattern.PathPatternParser; - -/** - * Simple wrapper around a {@link RouterFunctionMockMvcBuilder} that implements - * {@link MockMvcWebTestClient.RouterFunctionSpec}. - * - * @author Arjen Poutsma - * @since 6.2 - */ -class RouterFunctionMockMvcSpec extends AbstractMockMvcServerSpec - implements MockMvcWebTestClient.RouterFunctionSpec { - - private final RouterFunctionMockMvcBuilder mockMvcBuilder; - - - RouterFunctionMockMvcSpec(RouterFunction... routerFunctions) { - this.mockMvcBuilder = MockMvcBuilders.routerFunctions(routerFunctions); - } - - @Override - public MockMvcWebTestClient.RouterFunctionSpec messageConverters(HttpMessageConverter... messageConverters) { - this.mockMvcBuilder.setMessageConverters(messageConverters); - return this; - } - - @Override - public MockMvcWebTestClient.RouterFunctionSpec interceptors(HandlerInterceptor... interceptors) { - mappedInterceptors(null, interceptors); - return this; - } - - @Override - public MockMvcWebTestClient.RouterFunctionSpec mappedInterceptors(String @Nullable [] pathPatterns, HandlerInterceptor... interceptors) { - this.mockMvcBuilder.addMappedInterceptors(pathPatterns, interceptors); - return this; - } - - @Override - public MockMvcWebTestClient.RouterFunctionSpec asyncRequestTimeout(long timeout) { - this.mockMvcBuilder.setAsyncRequestTimeout(timeout); - return this; - } - - @Override - public MockMvcWebTestClient.RouterFunctionSpec handlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers) { - this.mockMvcBuilder.setHandlerExceptionResolvers(exceptionResolvers); - return this; - } - - @Override - public MockMvcWebTestClient.RouterFunctionSpec viewResolvers(ViewResolver... resolvers) { - this.mockMvcBuilder.setViewResolvers(resolvers); - return this; - } - - @Override - public MockMvcWebTestClient.RouterFunctionSpec singleView(View view) { - this.mockMvcBuilder.setSingleView(view); - return this; - } - - @Override - public MockMvcWebTestClient.RouterFunctionSpec patternParser(PathPatternParser parser) { - this.mockMvcBuilder.setPatternParser(parser); - return this; - } - - @Override - protected ConfigurableMockMvcBuilder getMockMvcBuilder() { - return this.mockMvcBuilder; - } -} diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/client/StandaloneMockMvcSpec.java b/spring-test/src/main/java/org/springframework/test/web/servlet/client/StandaloneMockMvcSpec.java deleted file mode 100644 index 9b96287e764..00000000000 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/client/StandaloneMockMvcSpec.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Copyright 2002-present 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. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.test.web.servlet.client; - -import java.util.function.Supplier; - -import org.jspecify.annotations.Nullable; - -import org.springframework.format.support.FormattingConversionService; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.test.web.servlet.setup.ConfigurableMockMvcBuilder; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder; -import org.springframework.validation.Validator; -import org.springframework.web.accept.ApiVersionStrategy; -import org.springframework.web.accept.ContentNegotiationManager; -import org.springframework.web.method.support.HandlerMethodArgumentResolver; -import org.springframework.web.method.support.HandlerMethodReturnValueHandler; -import org.springframework.web.servlet.FlashMapManager; -import org.springframework.web.servlet.HandlerExceptionResolver; -import org.springframework.web.servlet.HandlerInterceptor; -import org.springframework.web.servlet.LocaleResolver; -import org.springframework.web.servlet.View; -import org.springframework.web.servlet.ViewResolver; -import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; -import org.springframework.web.util.pattern.PathPatternParser; - -/** - * Simple wrapper around a {@link StandaloneMockMvcBuilder} that implements - * {@link MockMvcWebTestClient.ControllerSpec}. - * - * @author Rossen Stoyanchev - * @since 5.3 - */ -class StandaloneMockMvcSpec extends AbstractMockMvcServerSpec - implements MockMvcWebTestClient.ControllerSpec { - - private final StandaloneMockMvcBuilder mockMvcBuilder; - - - StandaloneMockMvcSpec(Object... controllers) { - this.mockMvcBuilder = MockMvcBuilders.standaloneSetup(controllers); - } - - @Override - public StandaloneMockMvcSpec controllerAdvice(Object... controllerAdvice) { - this.mockMvcBuilder.setControllerAdvice(controllerAdvice); - return this; - } - - @Override - public StandaloneMockMvcSpec messageConverters(HttpMessageConverter... messageConverters) { - this.mockMvcBuilder.setMessageConverters(messageConverters); - return this; - } - - @Override - public StandaloneMockMvcSpec validator(Validator validator) { - this.mockMvcBuilder.setValidator(validator); - return this; - } - - @Override - public StandaloneMockMvcSpec conversionService(FormattingConversionService conversionService) { - this.mockMvcBuilder.setConversionService(conversionService); - return this; - } - - @Override - public MockMvcWebTestClient.ControllerSpec apiVersionStrategy(ApiVersionStrategy versionStrategy) { - this.mockMvcBuilder.setApiVersionStrategy(versionStrategy); - return this; - } - - @Override - public StandaloneMockMvcSpec interceptors(HandlerInterceptor... interceptors) { - mappedInterceptors(null, interceptors); - return this; - } - - @Override - public StandaloneMockMvcSpec mappedInterceptors( - String @Nullable [] pathPatterns, HandlerInterceptor... interceptors) { - - this.mockMvcBuilder.addMappedInterceptors(pathPatterns, interceptors); - return this; - } - - @Override - public StandaloneMockMvcSpec contentNegotiationManager(ContentNegotiationManager manager) { - this.mockMvcBuilder.setContentNegotiationManager(manager); - return this; - } - - @Override - public StandaloneMockMvcSpec asyncRequestTimeout(long timeout) { - this.mockMvcBuilder.setAsyncRequestTimeout(timeout); - return this; - } - - @Override - public StandaloneMockMvcSpec customArgumentResolvers(HandlerMethodArgumentResolver... argumentResolvers) { - this.mockMvcBuilder.setCustomArgumentResolvers(argumentResolvers); - return this; - } - - @Override - public StandaloneMockMvcSpec customReturnValueHandlers(HandlerMethodReturnValueHandler... handlers) { - this.mockMvcBuilder.setCustomReturnValueHandlers(handlers); - return this; - } - - @Override - public StandaloneMockMvcSpec handlerExceptionResolvers(HandlerExceptionResolver... exceptionResolvers) { - this.mockMvcBuilder.setHandlerExceptionResolvers(exceptionResolvers); - return this; - } - - @Override - public StandaloneMockMvcSpec viewResolvers(ViewResolver... resolvers) { - this.mockMvcBuilder.setViewResolvers(resolvers); - return this; - } - - @Override - public StandaloneMockMvcSpec singleView(View view) { - this.mockMvcBuilder.setSingleView(view); - return this; - } - - @Override - public StandaloneMockMvcSpec localeResolver(LocaleResolver localeResolver) { - this.mockMvcBuilder.setLocaleResolver(localeResolver); - return this; - } - - @Override - public StandaloneMockMvcSpec flashMapManager(FlashMapManager flashMapManager) { - this.mockMvcBuilder.setFlashMapManager(flashMapManager); - return this; - } - - @Override - public StandaloneMockMvcSpec patternParser(PathPatternParser parser) { - this.mockMvcBuilder.setPatternParser(parser); - return this; - } - - @Override - public StandaloneMockMvcSpec placeholderValue(String name, String value) { - this.mockMvcBuilder.addPlaceholderValue(name, value); - return this; - } - - @Override - public StandaloneMockMvcSpec customHandlerMapping(Supplier factory) { - this.mockMvcBuilder.setCustomHandlerMapping(factory); - return this; - } - - @Override - public ConfigurableMockMvcBuilder getMockMvcBuilder() { - return this.mockMvcBuilder; - } -}