diff --git a/spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java b/spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java index e2cf0fae8f5..bf5f0b37ba8 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java +++ b/spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2025 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. @@ -18,6 +18,7 @@ package org.springframework.mock.web.server; import reactor.core.publisher.Mono; +import org.springframework.context.ApplicationContext; import org.springframework.http.codec.ServerCodecConfigurer; import org.springframework.lang.Nullable; import org.springframework.mock.http.server.reactive.MockServerHttpRequest; @@ -39,9 +40,15 @@ import org.springframework.web.server.session.WebSessionManager; */ public final class MockServerWebExchange extends DefaultServerWebExchange { - private MockServerWebExchange(MockServerHttpRequest request, WebSessionManager sessionManager) { - super(request, new MockServerHttpResponse(), sessionManager, - ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver()); + + private MockServerWebExchange( + MockServerHttpRequest request, @Nullable WebSessionManager sessionManager, + @Nullable ApplicationContext applicationContext) { + + super(request, new MockServerHttpResponse(), + sessionManager != null ? sessionManager : new DefaultWebSessionManager(), + ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver(), + applicationContext); } @@ -101,6 +108,9 @@ public final class MockServerWebExchange extends DefaultServerWebExchange { @Nullable private WebSessionManager sessionManager; + @Nullable + private ApplicationContext applicationContext; + public Builder(MockServerHttpRequest request) { this.request = request; } @@ -127,12 +137,21 @@ public final class MockServerWebExchange extends DefaultServerWebExchange { return this; } + /** + * Provide the {@code ApplicationContext} to expose through the exchange. + * @param applicationContext the context to use + * @since 6.2.5 + */ + public Builder applicationContext(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + return this; + } + /** * Build the {@code MockServerWebExchange} instance. */ public MockServerWebExchange build() { - return new MockServerWebExchange(this.request, - this.sessionManager != null ? this.sessionManager : new DefaultWebSessionManager()); + return new MockServerWebExchange(this.request, this.sessionManager, this.applicationContext); } } diff --git a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java index 0500180897b..0c84ef9d005 100644 --- a/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java +++ b/spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -119,7 +119,7 @@ public class DefaultServerWebExchange implements ServerWebExchange { this(request, response, sessionManager, codecConfigurer, localeContextResolver, null); } - DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response, + protected DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response, WebSessionManager sessionManager, ServerCodecConfigurer codecConfigurer, LocaleContextResolver localeContextResolver, @Nullable ApplicationContext applicationContext) {