Browse Source
SPR-12429 introduced various `BEFORE_*` modes in `@DirtiesContext`. To support these new modes, `DirtiesContextTestExecutionListener` (DCTEL) was updated to support both `BEFORE_*` and `AFTER_*` modes. However, there is a problem with having DCTEL support `BEFORE_*` modes since it is typically configured to execute after the `DependencyInjectionTestExecutionListener` (DITEL), and this leads to several undesired side effects: - The test's `ApplicationContext` is closed by DCTEL *after* dependencies have been injected into the test instance. - Injected dependencies may therefore attempt to interact with an `ApplicationContext` that is no longer _active_. - If a test has its `ApplicationContext` injected as a dependency, interaction with the context will likely fail since the context has been closed. - Any `TestExecutionListeners` registered after DCTEL will get a _new_ `ApplicationContext` if they invoke `getApplicationContext()` on the `TestContext`. This commit fixes these issues by introducing a new `DirtiesContextBeforeModesTestExecutionListener` (DCBMTEL) that is registered by default before DITEL. The previous support for `BEFORE_*` modes has been moved from DCTEL to DCBMTEL. In addition, an `AbstractDirtiesContextTestExecutionListener` has been extracted from DCTEL in order to avoid code duplication. Issue: SPR-13180pull/833/head
10 changed files with 531 additions and 230 deletions
@ -0,0 +1,151 @@
@@ -0,0 +1,151 @@
|
||||
/* |
||||
* Copyright 2002-2015 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 |
||||
* |
||||
* http://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.context.support; |
||||
|
||||
import java.lang.reflect.Method; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.core.annotation.AnnotatedElementUtils; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode; |
||||
import org.springframework.test.annotation.DirtiesContext.HierarchyMode; |
||||
import org.springframework.test.annotation.DirtiesContext.MethodMode; |
||||
import org.springframework.test.context.TestContext; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Abstract base class for {@code TestExecutionListener} implementations that |
||||
* provide support for marking the {@code ApplicationContext} associated with |
||||
* a test as <em>dirty</em> for both test classes and test methods annotated |
||||
* with the {@link DirtiesContext @DirtiesContext} annotation. |
||||
* |
||||
* <p>The core functionality for this class was extracted from |
||||
* {@link DirtiesContextTestExecutionListener} in Spring Framework 4.2. |
||||
* |
||||
* @author Sam Brannen |
||||
* @author Juergen Hoeller |
||||
* @since 4.2 |
||||
* @see DirtiesContext |
||||
*/ |
||||
public abstract class AbstractDirtiesContextTestExecutionListener extends AbstractTestExecutionListener { |
||||
|
||||
private static final Log logger = LogFactory.getLog(AbstractDirtiesContextTestExecutionListener.class); |
||||
|
||||
|
||||
@Override |
||||
public abstract int getOrder(); |
||||
|
||||
/** |
||||
* Mark the {@linkplain ApplicationContext application context} of the supplied |
||||
* {@linkplain TestContext test context} as |
||||
* {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty} |
||||
* and set {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE |
||||
* REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}. |
||||
* @param testContext the test context whose application context should |
||||
* be marked as dirty |
||||
* @param hierarchyMode the context cache clearing mode to be applied if the |
||||
* context is part of a hierarchy; may be {@code null} |
||||
* @since 3.2.2 |
||||
*/ |
||||
protected void dirtyContext(TestContext testContext, HierarchyMode hierarchyMode) { |
||||
testContext.markApplicationContextDirty(hierarchyMode); |
||||
testContext.setAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE, Boolean.TRUE); |
||||
} |
||||
|
||||
/** |
||||
* Perform the actual work for {@link #beforeTestMethod} and {@link #afterTestMethod} |
||||
* by dirtying the context if appropriate (i.e., according to the required modes). |
||||
* @param testContext the test context whose application context should |
||||
* potentially be marked as dirty; never {@code null} |
||||
* @param requiredMethodMode the method mode required for a context to |
||||
* be marked dirty in the current phase; never {@code null} |
||||
* @param requiredClassMode the class mode required for a context to |
||||
* be marked dirty in the current phase; never {@code null} |
||||
* @throws Exception allows any exception to propagate |
||||
* @since 4.2 |
||||
* @see #dirtyContext |
||||
*/ |
||||
protected void beforeOrAfterTestMethod(TestContext testContext, MethodMode requiredMethodMode, |
||||
ClassMode requiredClassMode) throws Exception { |
||||
|
||||
Assert.notNull(testContext, "TestContext must not be null"); |
||||
Assert.notNull(requiredMethodMode, "requiredMethodMode must not be null"); |
||||
Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); |
||||
|
||||
Class<?> testClass = testContext.getTestClass(); |
||||
Method testMethod = testContext.getTestMethod(); |
||||
Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); |
||||
Assert.notNull(testMethod, "The test method of the supplied TestContext must not be null"); |
||||
|
||||
DirtiesContext methodAnn = AnnotatedElementUtils.findMergedAnnotation(testMethod, DirtiesContext.class); |
||||
DirtiesContext classAnn = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); |
||||
boolean methodAnnotated = (methodAnn != null); |
||||
boolean classAnnotated = (classAnn != null); |
||||
MethodMode methodMode = (methodAnnotated ? methodAnn.methodMode() : null); |
||||
ClassMode classMode = (classAnnotated ? classAnn.classMode() : null); |
||||
|
||||
if (logger.isDebugEnabled()) { |
||||
String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); |
||||
logger.debug(String.format("%s test method: context %s, class annotated with @DirtiesContext [%s] " |
||||
+ "with mode [%s], method annotated with @DirtiesContext [%s] with mode [%s].", phase, testContext, |
||||
classAnnotated, classMode, methodAnnotated, methodMode)); |
||||
} |
||||
|
||||
if ((methodMode == requiredMethodMode) || (classMode == requiredClassMode)) { |
||||
HierarchyMode hierarchyMode = (methodAnnotated ? methodAnn.hierarchyMode() : classAnn.hierarchyMode()); |
||||
dirtyContext(testContext, hierarchyMode); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Perform the actual work for {@link #beforeTestClass} and {@link #afterTestClass} |
||||
* by dirtying the context if appropriate (i.e., according to the required mode). |
||||
* @param testContext the test context whose application context should |
||||
* potentially be marked as dirty; never {@code null} |
||||
* @param requiredClassMode the class mode required for a context to |
||||
* be marked dirty in the current phase; never {@code null} |
||||
* @throws Exception allows any exception to propagate |
||||
* @since 4.2 |
||||
* @see #dirtyContext |
||||
*/ |
||||
protected void beforeOrAfterTestClass(TestContext testContext, ClassMode requiredClassMode) throws Exception { |
||||
Assert.notNull(testContext, "TestContext must not be null"); |
||||
Assert.notNull(requiredClassMode, "requiredClassMode must not be null"); |
||||
|
||||
Class<?> testClass = testContext.getTestClass(); |
||||
Assert.notNull(testClass, "The test class of the supplied TestContext must not be null"); |
||||
|
||||
DirtiesContext dirtiesContext = AnnotatedElementUtils.findMergedAnnotation(testClass, DirtiesContext.class); |
||||
boolean classAnnotated = (dirtiesContext != null); |
||||
ClassMode classMode = (classAnnotated ? dirtiesContext.classMode() : null); |
||||
|
||||
if (logger.isDebugEnabled()) { |
||||
String phase = (requiredClassMode.name().startsWith("BEFORE") ? "Before" : "After"); |
||||
logger.debug(String.format( |
||||
"%s test class: context %s, class annotated with @DirtiesContext [%s] with mode [%s].", phase, |
||||
testContext, classAnnotated, classMode)); |
||||
} |
||||
|
||||
if (classMode == requiredClassMode) { |
||||
dirtyContext(testContext, dirtiesContext.hierarchyMode()); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,98 @@
@@ -0,0 +1,98 @@
|
||||
/* |
||||
* Copyright 2002-2015 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 |
||||
* |
||||
* http://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.context.support; |
||||
|
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.annotation.DirtiesContext.ClassMode; |
||||
import org.springframework.test.annotation.DirtiesContext.MethodMode; |
||||
import org.springframework.test.context.TestContext; |
||||
import org.springframework.test.context.TestExecutionListeners; |
||||
|
||||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.*; |
||||
import static org.springframework.test.annotation.DirtiesContext.MethodMode.*; |
||||
|
||||
/** |
||||
* {@code TestExecutionListener} which provides support for marking the |
||||
* {@code ApplicationContext} associated with a test as <em>dirty</em> for |
||||
* both test classes and test methods annotated with the |
||||
* {@link DirtiesContext @DirtiesContext} annotation. |
||||
* |
||||
* <p>This listener supports test methods with the |
||||
* {@linkplain DirtiesContext#methodMode method mode} set to |
||||
* {@link MethodMode#BEFORE_METHOD BEFORE_METHOD} and test classes with the |
||||
* {@linkplain DirtiesContext#classMode() class mode} set to |
||||
* {@link ClassMode#BEFORE_EACH_TEST_METHOD BEFORE_EACH_TEST_METHOD} or |
||||
* {@link ClassMode#BEFORE_CLASS BEFORE_CLASS}. For support for <em>AFTER</em> |
||||
* modes, see {@link DirtiesContextTestExecutionListener}. |
||||
* |
||||
* <p>When {@linkplain TestExecutionListeners#mergeMode merging} |
||||
* {@code TestExecutionListeners} with the defaults, this listener will |
||||
* automatically be ordered before the {@link DependencyInjectionTestExecutionListener}; |
||||
* otherwise, this listener must be manually configured to execute before the |
||||
* {@code DependencyInjectionTestExecutionListener}. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 4.2 |
||||
* @see DirtiesContext |
||||
* @see DirtiesContextTestExecutionListener |
||||
*/ |
||||
public class DirtiesContextBeforeModesTestExecutionListener extends AbstractDirtiesContextTestExecutionListener { |
||||
|
||||
/** |
||||
* Returns {@code 1500}. |
||||
*/ |
||||
@Override |
||||
public final int getOrder() { |
||||
return 1500; |
||||
} |
||||
|
||||
/** |
||||
* If the test class of the supplied {@linkplain TestContext test context} |
||||
* is annotated with {@code @DirtiesContext} and the {@linkplain |
||||
* DirtiesContext#classMode() class mode} is set to {@link |
||||
* ClassMode#BEFORE_CLASS BEFORE_CLASS}, the {@linkplain ApplicationContext |
||||
* application context} of the test context will be |
||||
* {@linkplain TestContext#markApplicationContextDirty marked as dirty}, and the |
||||
* {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE |
||||
* REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to |
||||
* {@code true}. |
||||
*/ |
||||
@Override |
||||
public void beforeTestClass(TestContext testContext) throws Exception { |
||||
beforeOrAfterTestClass(testContext, BEFORE_CLASS); |
||||
} |
||||
|
||||
/** |
||||
* If the current test method of the supplied {@linkplain TestContext test |
||||
* context} is annotated with {@code @DirtiesContext} and the {@linkplain |
||||
* DirtiesContext#methodMode() method mode} is set to {@link |
||||
* MethodMode#BEFORE_METHOD BEFORE_METHOD}, or if the test class is |
||||
* annotated with {@code @DirtiesContext} and the {@linkplain |
||||
* DirtiesContext#classMode() class mode} is set to {@link |
||||
* ClassMode#BEFORE_EACH_TEST_METHOD BEFORE_EACH_TEST_METHOD}, the |
||||
* {@linkplain ApplicationContext application context} of the test context |
||||
* will be {@linkplain TestContext#markApplicationContextDirty marked as dirty} and the |
||||
* {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE |
||||
* REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to {@code true}. |
||||
*/ |
||||
@Override |
||||
public void beforeTestMethod(TestContext testContext) throws Exception { |
||||
beforeOrAfterTestMethod(testContext, BEFORE_METHOD, BEFORE_EACH_TEST_METHOD); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,111 @@
@@ -0,0 +1,111 @@
|
||||
/* |
||||
* Copyright 2002-2015 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 |
||||
* |
||||
* http://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.context.cache; |
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
import org.junit.FixMethodOrder; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
import org.junit.runners.MethodSorters; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.context.ConfigurableApplicationContext; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.context.ContextConfiguration; |
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; |
||||
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener; |
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.springframework.test.annotation.DirtiesContext.MethodMode.*; |
||||
|
||||
/** |
||||
* Integration test which verifies correct interaction between the |
||||
* {@link DirtiesContextBeforeModesTestExecutionListener}, |
||||
* {@link DependencyInjectionTestExecutionListener}, and |
||||
* {@link DirtiesContextTestExecutionListener} when |
||||
* {@link DirtiesContext @DirtiesContext} is used at the method level. |
||||
* |
||||
* @author Sam Brannen |
||||
* @since 4.2 |
||||
*/ |
||||
@RunWith(SpringJUnit4ClassRunner.class) |
||||
@ContextConfiguration |
||||
@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
||||
public class MethodLevelDirtiesContextTests { |
||||
|
||||
private static final AtomicInteger contextCount = new AtomicInteger(); |
||||
|
||||
|
||||
@Configuration |
||||
static class Config { |
||||
|
||||
@Bean |
||||
Integer count() { |
||||
return contextCount.incrementAndGet(); |
||||
} |
||||
} |
||||
|
||||
|
||||
@Autowired |
||||
private ConfigurableApplicationContext context; |
||||
|
||||
@Autowired |
||||
private Integer count; |
||||
|
||||
|
||||
@Test |
||||
// test## prefix required for @FixMethodOrder.
|
||||
public void test01() throws Exception { |
||||
performAssertions(1); |
||||
} |
||||
|
||||
@Test |
||||
@DirtiesContext(methodMode = BEFORE_METHOD) |
||||
// test## prefix required for @FixMethodOrder.
|
||||
public void test02_dirtyContextBeforeTestMethod() throws Exception { |
||||
performAssertions(2); |
||||
} |
||||
|
||||
@Test |
||||
@DirtiesContext |
||||
// test## prefix required for @FixMethodOrder.
|
||||
public void test03_dirtyContextAferTestMethod() throws Exception { |
||||
performAssertions(2); |
||||
} |
||||
|
||||
@Test |
||||
// test## prefix required for @FixMethodOrder.
|
||||
public void test04() throws Exception { |
||||
performAssertions(3); |
||||
} |
||||
|
||||
private void performAssertions(int expectedContextCreationCount) throws Exception { |
||||
assertNotNull("context must not be null", this.context); |
||||
assertTrue("context must be active", this.context.isActive()); |
||||
|
||||
assertNotNull("count must not be null", this.count); |
||||
assertEquals("count: ", expectedContextCreationCount, this.count.intValue()); |
||||
|
||||
assertEquals("context creation count: ", expectedContextCreationCount, contextCount.get()); |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue