Skip to content

Commit 38ad39f

Browse files
committed
Do not repopulate RequestContextHolder in ServTEL
This commit fixes a bug introduced in the last commit. ServletTestExecutionListener (STEL) now tracks whether it has already populated the RequestContextHolder. Issue: SPR-11144 Backport-Commit: 800018a
1 parent 6ded287 commit 38ad39f

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

spring-test/src/main/java/org/springframework/test/context/web/ServletTestExecutionListener.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23-
2423
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2524
import org.springframework.context.ApplicationContext;
2625
import org.springframework.context.ConfigurableApplicationContext;
@@ -74,6 +73,16 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
7473
public static final String RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE = Conventions.getQualifiedAttributeName(
7574
ServletTestExecutionListener.class, "resetRequestContextHolder");
7675

76+
/**
77+
* Attribute name for a {@link TestContext} attribute which indicates that
78+
* {@code ServletTestExecutionListener} has already populated Spring Web's
79+
* {@code RequestContextHolder}.
80+
*
81+
* <p>Permissible values include {@link Boolean#TRUE} and {@link Boolean#FALSE}.
82+
*/
83+
public static final String POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE = Conventions.getQualifiedAttributeName(
84+
ServletTestExecutionListener.class, "populatedRequestContextHolder");
85+
7786
private static final Log logger = LogFactory.getLog(ServletTestExecutionListener.class);
7887

7988

@@ -111,8 +120,10 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
111120
* {@code RequestContextHolder}, but only if the {@link
112121
* #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} in the supplied {@code TestContext}
113122
* has a value of {@link Boolean#TRUE}.
114-
* <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be
115-
* subsequently removed from the test context, regardless of its value.
123+
*
124+
* <p>The {@link #RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} and
125+
* {@link #POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE} will be subsequently
126+
* removed from the test context, regardless of their values.
116127
*
117128
* @see TestExecutionListener#afterTestMethod(TestContext)
118129
*/
@@ -123,15 +134,20 @@ public void afterTestMethod(TestContext testContext) throws Exception {
123134
}
124135
RequestContextHolder.resetRequestAttributes();
125136
}
137+
testContext.removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
126138
testContext.removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
127139
}
128140

129141
private boolean notAnnotatedWithWebAppConfiguration(TestContext testContext) {
130142
return AnnotationUtils.findAnnotation(testContext.getTestClass(), WebAppConfiguration.class) == null;
131143
}
132144

145+
private boolean alreadyPopulatedRequestContextHolder(TestContext testContext) {
146+
return Boolean.TRUE.equals(testContext.getAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE));
147+
}
148+
133149
private void setUpRequestContextIfNecessary(TestContext testContext) {
134-
if (notAnnotatedWithWebAppConfiguration(testContext)) {
150+
if (notAnnotatedWithWebAppConfiguration(testContext) || alreadyPopulatedRequestContextHolder(testContext)) {
135151
return;
136152
}
137153

@@ -156,6 +172,7 @@ private void setUpRequestContextIfNecessary(TestContext testContext) {
156172
ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);
157173

158174
RequestContextHolder.setRequestAttributes(servletWebRequest);
175+
testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
159176
testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
160177

161178
if (wac instanceof ConfigurableApplicationContext) {

spring-test/src/test/java/org/springframework/test/context/web/ServletTestExecutionListenerTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,18 @@ public void atWebAppConfigTestCaseWithPresetRequestAttributes() throws Exception
175175
private void assertWebAppConfigTestCase() throws Exception {
176176
listener.prepareTestInstance(testContext);
177177
assertAttributeDoesNotExist();
178+
verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
178179
verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
180+
when(testContext.getAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(Boolean.TRUE);
179181
when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(Boolean.TRUE);
180182

181183
listener.beforeTestMethod(testContext);
182184
assertAttributeDoesNotExist();
183-
verify(testContext, times(2)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
185+
verify(testContext, times(1)).setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
186+
verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
184187

185188
listener.afterTestMethod(testContext);
189+
verify(testContext).removeAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
186190
verify(testContext).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
187191
assertAttributesNotAvailable();
188192
}

0 commit comments

Comments
 (0)