Skip to content

Commit 37d7af4

Browse files
committed
Allow setting ApplicationContext on MockServerWebExchange
Closes gh-34601
1 parent cc986cd commit 37d7af4

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

Diff for: spring-test/src/main/java/org/springframework/mock/web/server/MockServerWebExchange.java

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import reactor.core.publisher.Mono;
2020

21+
import org.springframework.context.ApplicationContext;
2122
import org.springframework.http.codec.ServerCodecConfigurer;
2223
import org.springframework.lang.Nullable;
2324
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
@@ -39,9 +40,15 @@
3940
*/
4041
public final class MockServerWebExchange extends DefaultServerWebExchange {
4142

42-
private MockServerWebExchange(MockServerHttpRequest request, WebSessionManager sessionManager) {
43-
super(request, new MockServerHttpResponse(), sessionManager,
44-
ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver());
43+
44+
private MockServerWebExchange(
45+
MockServerHttpRequest request, @Nullable WebSessionManager sessionManager,
46+
@Nullable ApplicationContext applicationContext) {
47+
48+
super(request, new MockServerHttpResponse(),
49+
sessionManager != null ? sessionManager : new DefaultWebSessionManager(),
50+
ServerCodecConfigurer.create(), new AcceptHeaderLocaleContextResolver(),
51+
applicationContext);
4552
}
4653

4754

@@ -101,6 +108,9 @@ public static class Builder {
101108
@Nullable
102109
private WebSessionManager sessionManager;
103110

111+
@Nullable
112+
private ApplicationContext applicationContext;
113+
104114
public Builder(MockServerHttpRequest request) {
105115
this.request = request;
106116
}
@@ -127,12 +137,21 @@ public Builder sessionManager(WebSessionManager sessionManager) {
127137
return this;
128138
}
129139

140+
/**
141+
* Provide the {@code ApplicationContext} to expose through the exchange.
142+
* @param applicationContext the context to use
143+
* @since 6.2.5
144+
*/
145+
public Builder applicationContext(ApplicationContext applicationContext) {
146+
this.applicationContext = applicationContext;
147+
return this;
148+
}
149+
130150
/**
131151
* Build the {@code MockServerWebExchange} instance.
132152
*/
133153
public MockServerWebExchange build() {
134-
return new MockServerWebExchange(this.request,
135-
this.sessionManager != null ? this.sessionManager : new DefaultWebSessionManager());
154+
return new MockServerWebExchange(this.request, this.sessionManager, this.applicationContext);
136155
}
137156
}
138157

Diff for: spring-web/src/main/java/org/springframework/web/server/adapter/DefaultServerWebExchange.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -119,7 +119,7 @@ public DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse re
119119
this(request, response, sessionManager, codecConfigurer, localeContextResolver, null);
120120
}
121121

122-
DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response,
122+
protected DefaultServerWebExchange(ServerHttpRequest request, ServerHttpResponse response,
123123
WebSessionManager sessionManager, ServerCodecConfigurer codecConfigurer,
124124
LocaleContextResolver localeContextResolver, @Nullable ApplicationContext applicationContext) {
125125

0 commit comments

Comments
 (0)