Honor presence of @WebAppConfiguration in ServTEL
The previous commit for issue SPR-11144 revealed a bug in ServletTestExecutionListener (STEL). Specifically, STEL acted on the fact that the ApplicationContext for a given TestContext was an instance of WebApplicationContext. This behavior could potentially break test code from previous releases of the Spring Framework that relied on a custom setup of the RequestAttributes in the RequestContextHolder with a custom WebApplicationContext ContextLoader. This commit addresses this issue by ensuring that STEL only comes into play if the test class is annotated with @WebAppConfiguration (for prepareTestInstance() and beforeTestMethod()) or if the TestContext attribute named RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE is set to Boolean.TRUE (for afterTestMethod()). Issue: SPR-11144
This commit is contained in:
@@ -18,6 +18,7 @@ package org.springframework.test.context.web;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
@@ -48,6 +49,14 @@ public class ServletTestExecutionListenerTests {
|
||||
private final ServletTestExecutionListener listener = new ServletTestExecutionListener();
|
||||
|
||||
|
||||
private void assertAttributesAvailable() {
|
||||
assertNotNull("request attributes should be available", RequestContextHolder.getRequestAttributes());
|
||||
}
|
||||
|
||||
private void assertAttributesNotAvailable() {
|
||||
assertNull("request attributes should not be available", RequestContextHolder.getRequestAttributes());
|
||||
}
|
||||
|
||||
private void assertAttributeExists() {
|
||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||
assertNotNull("request attributes should exist", requestAttributes);
|
||||
@@ -80,9 +89,13 @@ public class ServletTestExecutionListenerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withStandardApplicationContext() throws Exception {
|
||||
public void standardApplicationContext() throws Exception {
|
||||
Mockito.<Class<?>> when(testContext.getTestClass()).thenReturn(getClass());
|
||||
when(testContext.getApplicationContext()).thenReturn(mock(ApplicationContext.class));
|
||||
|
||||
listener.beforeTestClass(testContext);
|
||||
assertAttributeExists();
|
||||
|
||||
listener.prepareTestInstance(testContext);
|
||||
assertAttributeExists();
|
||||
|
||||
@@ -94,40 +107,92 @@ public class ServletTestExecutionListenerTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withWebApplicationContextWithoutExistingRequestAttributes() throws Exception {
|
||||
assertAttributeExists();
|
||||
public void legacyWebTestCaseWithoutExistingRequestAttributes() throws Exception {
|
||||
Mockito.<Class<?>> when(testContext.getTestClass()).thenReturn(LegacyWebTestCase.class);
|
||||
|
||||
RequestContextHolder.resetRequestAttributes();
|
||||
assertAttributesNotAvailable();
|
||||
|
||||
listener.beforeTestClass(testContext);
|
||||
|
||||
listener.prepareTestInstance(testContext);
|
||||
assertAttributesNotAvailable();
|
||||
verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(null);
|
||||
|
||||
listener.beforeTestMethod(testContext);
|
||||
assertAttributesNotAvailable();
|
||||
verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
|
||||
listener.afterTestMethod(testContext);
|
||||
verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
|
||||
assertAttributesNotAvailable();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void legacyWebTestCaseWithPresetRequestAttributes() throws Exception {
|
||||
Mockito.<Class<?>> when(testContext.getTestClass()).thenReturn(LegacyWebTestCase.class);
|
||||
|
||||
listener.beforeTestClass(testContext);
|
||||
assertAttributeExists();
|
||||
|
||||
listener.prepareTestInstance(testContext);
|
||||
assertAttributeExists();
|
||||
verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(null);
|
||||
|
||||
listener.beforeTestMethod(testContext);
|
||||
assertAttributeExists();
|
||||
verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(null);
|
||||
|
||||
listener.afterTestMethod(testContext);
|
||||
verify(testContext, times(1)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
|
||||
assertAttributeExists();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atWebAppConfigTestCaseWithoutExistingRequestAttributes() throws Exception {
|
||||
Mockito.<Class<?>> when(testContext.getTestClass()).thenReturn(AtWebAppConfigWebTestCase.class);
|
||||
|
||||
RequestContextHolder.resetRequestAttributes();
|
||||
listener.beforeTestClass(testContext);
|
||||
assertAttributesNotAvailable();
|
||||
|
||||
assertWebAppConfigTestCase();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void atWebAppConfigTestCaseWithPresetRequestAttributes() throws Exception {
|
||||
Mockito.<Class<?>> when(testContext.getTestClass()).thenReturn(AtWebAppConfigWebTestCase.class);
|
||||
|
||||
listener.beforeTestClass(testContext);
|
||||
assertAttributesAvailable();
|
||||
|
||||
assertWebAppConfigTestCase();
|
||||
}
|
||||
|
||||
private void assertWebAppConfigTestCase() throws Exception {
|
||||
listener.prepareTestInstance(testContext);
|
||||
assertAttributeDoesNotExist();
|
||||
verify(testContext).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
verify(testContext, times(1)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(Boolean.TRUE);
|
||||
|
||||
listener.beforeTestMethod(testContext);
|
||||
assertAttributeDoesNotExist();
|
||||
verify(testContext).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
verify(testContext, times(2)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
|
||||
listener.afterTestMethod(testContext);
|
||||
verify(testContext).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
|
||||
assertNull("request attributes should have been cleared", RequestContextHolder.getRequestAttributes());
|
||||
assertAttributesNotAvailable();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withWebApplicationContextWithPresetRequestAttributes() throws Exception {
|
||||
assertAttributeExists();
|
||||
|
||||
listener.prepareTestInstance(testContext);
|
||||
assertAttributeExists();
|
||||
verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(null);
|
||||
static class LegacyWebTestCase {
|
||||
}
|
||||
|
||||
listener.beforeTestMethod(testContext);
|
||||
assertAttributeExists();
|
||||
verify(testContext, times(0)).setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
|
||||
when(testContext.getAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE)).thenReturn(null);
|
||||
|
||||
listener.afterTestMethod(testContext);
|
||||
verify(testContext, times(0)).removeAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE);
|
||||
@WebAppConfiguration
|
||||
static class AtWebAppConfigWebTestCase {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user