001/* 002 * Copyright 2002-2015 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.context.support; 018 019import org.springframework.context.ApplicationContext; 020import org.springframework.test.annotation.DirtiesContext; 021import org.springframework.test.annotation.DirtiesContext.ClassMode; 022import org.springframework.test.annotation.DirtiesContext.MethodMode; 023import org.springframework.test.context.TestContext; 024import org.springframework.test.context.TestExecutionListeners; 025 026import static org.springframework.test.annotation.DirtiesContext.ClassMode.*; 027import static org.springframework.test.annotation.DirtiesContext.MethodMode.*; 028 029/** 030 * {@code TestExecutionListener} which provides support for marking the 031 * {@code ApplicationContext} associated with a test as <em>dirty</em> for 032 * both test classes and test methods annotated with the 033 * {@link DirtiesContext @DirtiesContext} annotation. 034 * 035 * <p>This listener supports test methods with the 036 * {@linkplain DirtiesContext#methodMode method mode} set to 037 * {@link MethodMode#AFTER_METHOD AFTER_METHOD} and test classes with the 038 * {@linkplain DirtiesContext#classMode() class mode} set to 039 * {@link ClassMode#AFTER_EACH_TEST_METHOD AFTER_EACH_TEST_METHOD} or 040 * {@link ClassMode#AFTER_CLASS AFTER_CLASS}. For support for <em>BEFORE</em> 041 * modes, see {@link DirtiesContextBeforeModesTestExecutionListener}. 042 * 043 * <p>When {@linkplain TestExecutionListeners#mergeMode merging} 044 * {@code TestExecutionListeners} with the defaults, this listener will 045 * automatically be ordered after the {@link DependencyInjectionTestExecutionListener}; 046 * otherwise, this listener must be manually configured to execute after the 047 * {@code DependencyInjectionTestExecutionListener}. 048 * 049 * @author Sam Brannen 050 * @since 2.5 051 * @see DirtiesContext 052 * @see DirtiesContextBeforeModesTestExecutionListener 053 */ 054public class DirtiesContextTestExecutionListener extends AbstractDirtiesContextTestExecutionListener { 055 056 /** 057 * Returns {@code 3000}. 058 */ 059 @Override 060 public final int getOrder() { 061 return 3000; 062 } 063 064 /** 065 * If the current test method of the supplied {@linkplain TestContext test 066 * context} is annotated with {@code @DirtiesContext} and the {@linkplain 067 * DirtiesContext#methodMode() method mode} is set to {@link 068 * MethodMode#AFTER_METHOD AFTER_METHOD}, or if the test class is 069 * annotated with {@code @DirtiesContext} and the {@linkplain 070 * DirtiesContext#classMode() class mode} is set to {@link 071 * ClassMode#AFTER_EACH_TEST_METHOD AFTER_EACH_TEST_METHOD}, the 072 * {@linkplain ApplicationContext application context} of the test context 073 * will be {@linkplain TestContext#markApplicationContextDirty marked as dirty} and the 074 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE 075 * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to {@code true}. 076 */ 077 @Override 078 public void afterTestMethod(TestContext testContext) throws Exception { 079 beforeOrAfterTestMethod(testContext, AFTER_METHOD, AFTER_EACH_TEST_METHOD); 080 } 081 082 /** 083 * If the test class of the supplied {@linkplain TestContext test context} 084 * is annotated with {@code @DirtiesContext} and the {@linkplain 085 * DirtiesContext#classMode() class mode} is set to {@link 086 * ClassMode#AFTER_CLASS AFTER_CLASS}, the {@linkplain ApplicationContext 087 * application context} of the test context will be 088 * {@linkplain TestContext#markApplicationContextDirty marked as dirty}, and the 089 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE 090 * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to 091 * {@code true}. 092 */ 093 @Override 094 public void afterTestClass(TestContext testContext) throws Exception { 095 beforeOrAfterTestClass(testContext, AFTER_CLASS); 096 } 097 098}