提交 0aac02d6 编写于 作者: S Sam Brannen

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
上级 3272a3b8
......@@ -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 {
/**
......
/*
* 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());
}
}
}
/*
* 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 */
......
# Default TestExecutionListeners for the Spring TestContext Framework
#
org.springframework.test.context.TestExecutionListener = \
org.springframework.test.context.web.ServletTestExecutionListener,\
org.springframework.test.context.web.ServletTestExecutionListener,\
org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener,\
org.springframework.test.context.support.DependencyInjectionTestExecutionListener,\
org.springframework.test.context.support.DirtiesContextTestExecutionListener,\
org.springframework.test.context.transaction.TransactionalTestExecutionListener,\
......
......@@ -19,7 +19,6 @@ package org.springframework.test.context;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Test;
......@@ -28,11 +27,13 @@ import org.springframework.core.annotation.AnnotationConfigurationException;
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
import org.springframework.test.context.support.AbstractTestExecutionListener;
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.transaction.TransactionalTestExecutionListener;
import org.springframework.test.context.web.ServletTestExecutionListener;
import static java.util.Arrays.*;
import static java.util.stream.Collectors.*;
import static org.junit.Assert.*;
import static org.springframework.test.context.TestExecutionListeners.MergeMode.*;
......@@ -40,7 +41,7 @@ import static org.springframework.test.context.TestExecutionListeners.MergeMode.
* Unit tests for the {@link TestExecutionListeners @TestExecutionListeners}
* annotation, which verify:
* <ul>
* <li>Proper registering of {@link TestExecutionListener listeners} in
* <li>Proper registering of {@linkplain TestExecutionListener listeners} in
* conjunction with a {@link TestContextManager}</li>
* <li><em>Inherited</em> functionality proposed in
* <a href="https://jira.spring.io/browse/SPR-3896" target="_blank">SPR-3896</a></li>
......@@ -51,26 +52,12 @@ import static org.springframework.test.context.TestExecutionListeners.MergeMode.
*/
public class TestExecutionListenersTests {
private List<Class<?>> classes(TestContextManager testContextManager) {
return testContextManager.getTestExecutionListeners().stream().map(listener -> listener.getClass()).collect(
Collectors.toList());
}
private List<String> names(List<Class<?>> classes) {
return classes.stream().map(clazz -> clazz.getSimpleName()).collect(Collectors.toList());
}
private void assertRegisteredListeners(Class<?> testClass, List<Class<?>> expected) {
TestContextManager testContextManager = new TestContextManager(testClass);
assertEquals("TELs registered for " + testClass.getSimpleName(), names(expected),
names(classes(testContextManager)));
}
@Test
public void defaultListeners() {
List<Class<?>> expected = asList(ServletTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, SqlScriptsTestExecutionListener.class);
DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class);
assertRegisteredListeners(DefaultListenersTestCase.class, expected);
}
......@@ -80,8 +67,9 @@ public class TestExecutionListenersTests {
@Test
public void defaultListenersMergedWithCustomListenerPrepended() {
List<Class<?>> expected = asList(QuuxTestExecutionListener.class, ServletTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, SqlScriptsTestExecutionListener.class);
DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class);
assertRegisteredListeners(MergedDefaultListenersWithCustomListenerPrependedTestCase.class, expected);
}
......@@ -91,9 +79,9 @@ public class TestExecutionListenersTests {
@Test
public void defaultListenersMergedWithCustomListenerAppended() {
List<Class<?>> expected = asList(ServletTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, SqlScriptsTestExecutionListener.class,
BazTestExecutionListener.class);
DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class, BazTestExecutionListener.class);
assertRegisteredListeners(MergedDefaultListenersWithCustomListenerAppendedTestCase.class, expected);
}
......@@ -103,9 +91,9 @@ public class TestExecutionListenersTests {
@Test
public void defaultListenersMergedWithCustomListenerInserted() {
List<Class<?>> expected = asList(ServletTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class, BarTestExecutionListener.class,
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
SqlScriptsTestExecutionListener.class);
DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
BarTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class, SqlScriptsTestExecutionListener.class);
assertRegisteredListeners(MergedDefaultListenersWithCustomListenerInsertedTestCase.class, expected);
}
......@@ -124,66 +112,47 @@ public class TestExecutionListenersTests {
@Test
public void customListeners() {
TestContextManager testContextManager = new TestContextManager(ExplicitListenersTestCase.class);
assertEquals("Num registered TELs for ExplicitListenersTestCase.", 3,
testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(ExplicitListenersTestCase.class, 3);
}
@Test
public void nonInheritedListeners() {
TestContextManager testContextManager = new TestContextManager(NonInheritedListenersTestCase.class);
assertEquals("Num registered TELs for NonInheritedListenersTestCase.", 1,
testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(NonInheritedListenersTestCase.class, 1);
}
@Test
public void inheritedListeners() {
TestContextManager testContextManager = new TestContextManager(InheritedListenersTestCase.class);
assertEquals("Num registered TELs for InheritedListenersTestCase.", 4,
testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(InheritedListenersTestCase.class, 4);
}
@Test
public void customListenersRegisteredViaMetaAnnotation() {
TestContextManager testContextManager = new TestContextManager(MetaTestCase.class);
assertEquals("Num registered TELs for MetaTestCase.", 3, testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(MetaTestCase.class, 3);
}
@Test
public void nonInheritedListenersRegisteredViaMetaAnnotation() {
TestContextManager testContextManager = new TestContextManager(MetaNonInheritedListenersTestCase.class);
assertEquals("Num registered TELs for MetaNonInheritedListenersTestCase.", 1,
testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(MetaNonInheritedListenersTestCase.class, 1);
}
@Test
public void inheritedListenersRegisteredViaMetaAnnotation() {
TestContextManager testContextManager = new TestContextManager(MetaInheritedListenersTestCase.class);
assertEquals("Num registered TELs for MetaInheritedListenersTestCase.", 4,
testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(MetaInheritedListenersTestCase.class, 4);
}
@Test
public void customListenersRegisteredViaMetaAnnotationWithOverrides() {
TestContextManager testContextManager = new TestContextManager(MetaWithOverridesTestCase.class);
assertEquals("Num registered TELs for MetaWithOverridesTestCase.", 3,
testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(MetaWithOverridesTestCase.class, 3);
}
@Test
public void customsListenersRegisteredViaMetaAnnotationWithInheritedListenersWithOverrides() {
TestContextManager testContextManager = new TestContextManager(
MetaInheritedListenersWithOverridesTestCase.class);
assertEquals("Num registered TELs for MetaInheritedListenersWithOverridesTestCase.", 5,
testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(MetaInheritedListenersWithOverridesTestCase.class, 5);
}
@Test
public void customListenersRegisteredViaMetaAnnotationWithNonInheritedListenersWithOverrides() {
TestContextManager testContextManager = new TestContextManager(
MetaNonInheritedListenersWithOverridesTestCase.class);
assertEquals("Num registered TELs for MetaNonInheritedListenersWithOverridesTestCase.", 8,
testContextManager.getTestExecutionListeners().size());
assertNumRegisteredListeners(MetaNonInheritedListenersWithOverridesTestCase.class, 8);
}
@Test(expected = AnnotationConfigurationException.class)
......@@ -192,6 +161,27 @@ public class TestExecutionListenersTests {
}
private List<Class<?>> classes(TestContextManager testContextManager) {
return testContextManager.getTestExecutionListeners().stream().map(Object::getClass).collect(toList());
}
private List<String> names(List<Class<?>> classes) {
return classes.stream().map(Class::getSimpleName).collect(toList());
}
private void assertRegisteredListeners(Class<?> testClass, List<Class<?>> expected) {
TestContextManager testContextManager = new TestContextManager(testClass);
assertEquals("TELs registered for " + testClass.getSimpleName(), names(expected),
names(classes(testContextManager)));
}
private void assertNumRegisteredListeners(Class<?> testClass, int expected) {
TestContextManager testContextManager = new TestContextManager(testClass);
assertEquals("Num registered TELs for " + testClass, expected,
testContextManager.getTestExecutionListeners().size());
}
// -------------------------------------------------------------------
static class DefaultListenersTestCase {
......
/*
* 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());
}
}
......@@ -27,22 +27,24 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.annotation.DirtiesContext.HierarchyMode;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import static org.mockito.BDDMockito.*;
import static org.springframework.test.annotation.DirtiesContext.ClassMode.*;
import static org.springframework.test.annotation.DirtiesContext.HierarchyMode.*;
import static org.springframework.test.annotation.DirtiesContext.MethodMode.*;
/**
* Unit tests for {@link DirtiesContextTestExecutionListener}.
* Unit tests for {@link DirtiesContextBeforeModesTestExecutionListener}.
* and {@link DirtiesContextTestExecutionListener}
*
* @author Sam Brannen
* @since 4.0
*/
public class DirtiesContextTestExecutionListenerTests {
private final DirtiesContextTestExecutionListener listener = new DirtiesContextTestExecutionListener();
private final TestExecutionListener beforeListener = new DirtiesContextBeforeModesTestExecutionListener();
private final TestExecutionListener afterListener = new DirtiesContextTestExecutionListener();
private final TestContext testContext = mock(TestContext.class);
......@@ -52,9 +54,11 @@ public class DirtiesContextTestExecutionListenerTests {
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(
clazz.getDeclaredMethod("dirtiesContextDeclaredLocallyWithBeforeMethodMode"));
listener.beforeTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
listener.afterTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -64,9 +68,11 @@ public class DirtiesContextTestExecutionListenerTests {
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(
clazz.getDeclaredMethod("dirtiesContextDeclaredLocallyWithAfterMethodMode"));
listener.beforeTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
listener.afterTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -77,9 +83,11 @@ public class DirtiesContextTestExecutionListenerTests {
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(
clazz.getDeclaredMethod("dirtiesContextDeclaredViaMetaAnnotationWithAfterMethodMode"));
listener.beforeTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
listener.afterTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -88,9 +96,11 @@ public class DirtiesContextTestExecutionListenerTests {
Class<?> clazz = DirtiesContextDeclaredLocallyBeforeEachTestMethod.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
listener.beforeTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
listener.afterTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -99,9 +109,11 @@ public class DirtiesContextTestExecutionListenerTests {
Class<?> clazz = DirtiesContextDeclaredLocallyAfterEachTestMethod.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
listener.beforeTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
listener.afterTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -111,9 +123,11 @@ public class DirtiesContextTestExecutionListenerTests {
Class<?> clazz = DirtiesContextDeclaredViaMetaAnnotationAfterEachTestMethod.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
listener.beforeTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
listener.afterTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -122,8 +136,10 @@ public class DirtiesContextTestExecutionListenerTests {
Class<?> clazz = DirtiesContextDeclaredLocallyBeforeClass.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
listener.beforeTestMethod(testContext);
listener.afterTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}
......@@ -132,8 +148,10 @@ public class DirtiesContextTestExecutionListenerTests {
Class<?> clazz = DirtiesContextDeclaredLocallyAfterClass.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
listener.beforeTestMethod(testContext);
listener.afterTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}
......@@ -142,8 +160,10 @@ public class DirtiesContextTestExecutionListenerTests {
Class<?> clazz = DirtiesContextDeclaredViaMetaAnnotationAfterClass.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
listener.beforeTestMethod(testContext);
listener.afterTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}
......@@ -152,9 +172,11 @@ public class DirtiesContextTestExecutionListenerTests {
Class<?> clazz = DirtiesContextViaMetaAnnotationWithOverrides.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
given(testContext.getTestMethod()).willReturn(clazz.getDeclaredMethod("clean"));
listener.beforeTestMethod(testContext);
beforeListener.beforeTestMethod(testContext);
afterListener.beforeTestMethod(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
listener.afterTestMethod(testContext);
afterListener.afterTestMethod(testContext);
beforeListener.afterTestMethod(testContext);
verify(testContext, times(1)).markApplicationContextDirty(CURRENT_LEVEL);
}
......@@ -164,8 +186,10 @@ public class DirtiesContextTestExecutionListenerTests {
public void beforeAndAfterTestClassForDirtiesContextDeclaredLocallyOnMethod() throws Exception {
Class<?> clazz = getClass();
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
listener.afterTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}
......@@ -173,8 +197,10 @@ public class DirtiesContextTestExecutionListenerTests {
public void beforeAndAfterTestClassForDirtiesContextDeclaredLocallyOnClassBeforeEachTestMethod() throws Exception {
Class<?> clazz = DirtiesContextDeclaredLocallyBeforeEachTestMethod.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
listener.afterTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}
......@@ -182,8 +208,10 @@ public class DirtiesContextTestExecutionListenerTests {
public void beforeAndAfterTestClassForDirtiesContextDeclaredLocallyOnClassAfterEachTestMethod() throws Exception {
Class<?> clazz = DirtiesContextDeclaredLocallyAfterEachTestMethod.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
listener.afterTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}
......@@ -192,8 +220,10 @@ public class DirtiesContextTestExecutionListenerTests {
throws Exception {
Class<?> clazz = DirtiesContextDeclaredViaMetaAnnotationAfterEachTestMethod.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
listener.afterTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}
......@@ -201,9 +231,11 @@ public class DirtiesContextTestExecutionListenerTests {
public void beforeAndAfterTestClassForDirtiesContextDeclaredLocallyOnClassBeforeClass() throws Exception {
Class<?> clazz = DirtiesContextDeclaredLocallyBeforeClass.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
listener.afterTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -211,9 +243,11 @@ public class DirtiesContextTestExecutionListenerTests {
public void beforeAndAfterTestClassForDirtiesContextDeclaredLocallyOnClassAfterClass() throws Exception {
Class<?> clazz = DirtiesContextDeclaredLocallyAfterClass.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
listener.afterTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -221,9 +255,11 @@ public class DirtiesContextTestExecutionListenerTests {
public void beforeAndAfterTestClassForDirtiesContextDeclaredViaMetaAnnotationOnClassAfterClass() throws Exception {
Class<?> clazz = DirtiesContextDeclaredViaMetaAnnotationAfterClass.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
listener.afterTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......@@ -231,8 +267,10 @@ public class DirtiesContextTestExecutionListenerTests {
public void beforeAndAfterTestClassForDirtiesContextDeclaredViaMetaAnnotationWithOverrides() throws Exception {
Class<?> clazz = DirtiesContextViaMetaAnnotationWithOverrides.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
listener.afterTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
}
......@@ -241,9 +279,11 @@ public class DirtiesContextTestExecutionListenerTests {
throws Exception {
Class<?> clazz = DirtiesContextViaMetaAnnotationWithOverridenAttributes.class;
BDDMockito.<Class<?>> given(testContext.getTestClass()).willReturn(clazz);
listener.beforeTestClass(testContext);
beforeListener.beforeTestClass(testContext);
afterListener.beforeTestClass(testContext);
verify(testContext, times(0)).markApplicationContextDirty(any(HierarchyMode.class));
listener.afterTestClass(testContext);
afterListener.afterTestClass(testContext);
beforeListener.afterTestClass(testContext);
verify(testContext, times(1)).markApplicationContextDirty(EXHAUSTIVE);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册