Introduce DirtiesContextBeforeModesTestExecutionListener
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-13180
This commit is contained in:
@@ -64,10 +64,16 @@ import java.lang.annotation.Target;
|
||||
* {@link ClassMode#AFTER_CLASS AFTER_CLASS}</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>{@code BEFORE_*} modes are supported by the
|
||||
* {@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener DirtiesContextBeforeModesTestExecutionListener};
|
||||
* {@code AFTER_*} modes are supported by the
|
||||
* {@link org.springframework.test.context.support.DirtiesContextTestExecutionListener DirtiesContextTestExecutionListener}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Rod Johnson
|
||||
* @since 2.0
|
||||
* @see org.springframework.test.context.ContextConfiguration
|
||||
* @see org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
|
||||
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
|
||||
*/
|
||||
@Documented
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
|
||||
@@ -51,6 +52,7 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
|
||||
* </ul>
|
||||
@@ -77,14 +79,15 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
* @see TestContextManager
|
||||
* @see TestExecutionListeners
|
||||
* @see ServletTestExecutionListener
|
||||
* @see DirtiesContextBeforeModesTestExecutionListener
|
||||
* @see DependencyInjectionTestExecutionListener
|
||||
* @see DirtiesContextTestExecutionListener
|
||||
* @see AbstractTransactionalJUnit4SpringContextTests
|
||||
* @see org.springframework.test.context.testng.AbstractTestNGSpringContextTests
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@TestExecutionListeners({ServletTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
|
||||
DirtiesContextTestExecutionListener.class})
|
||||
@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
|
||||
DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
|
||||
public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware {
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,19 +16,12 @@
|
||||
|
||||
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;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
|
||||
import static org.springframework.test.annotation.DirtiesContext.ClassMode.*;
|
||||
import static org.springframework.test.annotation.DirtiesContext.MethodMode.*;
|
||||
@@ -39,15 +32,26 @@ import static org.springframework.test.annotation.DirtiesContext.MethodMode.*;
|
||||
* 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#AFTER_METHOD AFTER_METHOD} and test classes with the
|
||||
* {@linkplain DirtiesContext#classMode() class mode} set to
|
||||
* {@link ClassMode#AFTER_EACH_TEST_METHOD AFTER_EACH_TEST_METHOD} or
|
||||
* {@link ClassMode#AFTER_CLASS AFTER_CLASS}. For support for <em>BEFORE</em>
|
||||
* modes, see {@link DirtiesContextBeforeModesTestExecutionListener}.
|
||||
*
|
||||
* <p>When {@linkplain TestExecutionListeners#mergeMode merging}
|
||||
* {@code TestExecutionListeners} with the defaults, this listener will
|
||||
* automatically be ordered after the {@link DependencyInjectionTestExecutionListener};
|
||||
* otherwise, this listener must be manually configured to execute after the
|
||||
* {@code DependencyInjectionTestExecutionListener}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5
|
||||
* @see DirtiesContext
|
||||
* @see DirtiesContextBeforeModesTestExecutionListener
|
||||
*/
|
||||
public class DirtiesContextTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(DirtiesContextTestExecutionListener.class);
|
||||
|
||||
public class DirtiesContextTestExecutionListener extends AbstractDirtiesContextTestExecutionListener {
|
||||
|
||||
/**
|
||||
* Returns {@code 3000}.
|
||||
@@ -57,41 +61,6 @@ public class DirtiesContextTestExecutionListener extends AbstractTestExecutionLi
|
||||
return 3000;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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", 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}.
|
||||
* @since 4.2
|
||||
*/
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
beforeOrAfterTestMethod(testContext, "Before", BEFORE_METHOD, BEFORE_EACH_TEST_METHOD);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the current test method of the supplied {@linkplain TestContext test
|
||||
* context} is annotated with {@code @DirtiesContext} and the {@linkplain
|
||||
@@ -107,7 +76,7 @@ public class DirtiesContextTestExecutionListener extends AbstractTestExecutionLi
|
||||
*/
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
beforeOrAfterTestMethod(testContext, "After", AFTER_METHOD, AFTER_EACH_TEST_METHOD);
|
||||
beforeOrAfterTestMethod(testContext, AFTER_METHOD, AFTER_EACH_TEST_METHOD);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,79 +92,7 @@ public class DirtiesContextTestExecutionListener extends AbstractTestExecutionLi
|
||||
*/
|
||||
@Override
|
||||
public void afterTestClass(TestContext testContext) throws Exception {
|
||||
beforeOrAfterTestClass(testContext, "After", AFTER_CLASS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the {@linkplain ApplicationContext application context} of the supplied
|
||||
* {@linkplain TestContext test context} as
|
||||
* {@linkplain TestContext#markApplicationContextDirty(DirtiesContext.HierarchyMode) dirty}
|
||||
* and sets {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE
|
||||
* REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context to {@code true}.
|
||||
* @param testContext the test context whose application context should
|
||||
* 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}.
|
||||
* @since 4.2
|
||||
*/
|
||||
private void beforeOrAfterTestMethod(TestContext testContext, String phase, MethodMode requiredMethodMode,
|
||||
ClassMode requiredClassMode) throws Exception {
|
||||
|
||||
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()) {
|
||||
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}.
|
||||
* @since 4.2
|
||||
*/
|
||||
private void beforeOrAfterTestClass(TestContext testContext, String phase, ClassMode requiredClassMode)
|
||||
throws Exception {
|
||||
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()) {
|
||||
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());
|
||||
}
|
||||
beforeOrAfterTestClass(testContext, AFTER_CLASS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,13 +21,6 @@ import java.lang.reflect.Method;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.testng.IHookCallBack;
|
||||
import org.testng.IHookable;
|
||||
import org.testng.ITestResult;
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
@@ -36,9 +29,18 @@ import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
|
||||
import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
|
||||
import org.testng.IHookCallBack;
|
||||
import org.testng.IHookable;
|
||||
import org.testng.ITestResult;
|
||||
import org.testng.annotations.AfterClass;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
|
||||
/**
|
||||
* Abstract base test class which integrates the <em>Spring TestContext Framework</em>
|
||||
* with explicit {@link ApplicationContext} testing support in a <strong>TestNG</strong>
|
||||
@@ -64,6 +66,7 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
|
||||
* </ul>
|
||||
@@ -76,13 +79,14 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
* @see TestContextManager
|
||||
* @see TestExecutionListeners
|
||||
* @see ServletTestExecutionListener
|
||||
* @see DirtiesContextBeforeModesTestExecutionListener
|
||||
* @see DependencyInjectionTestExecutionListener
|
||||
* @see DirtiesContextTestExecutionListener
|
||||
* @see AbstractTransactionalTestNGSpringContextTests
|
||||
* @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
|
||||
*/
|
||||
@TestExecutionListeners({ServletTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
|
||||
DirtiesContextTestExecutionListener.class})
|
||||
@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
|
||||
DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class })
|
||||
public abstract class AbstractTestNGSpringContextTests implements IHookable, ApplicationContextAware {
|
||||
|
||||
/** Logger available to subclasses */
|
||||
|
||||
Reference in New Issue
Block a user