001/* 002 * Copyright 2002-2018 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.AFTER_CLASS; 027import static org.springframework.test.annotation.DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD; 028import static org.springframework.test.annotation.DirtiesContext.MethodMode.AFTER_METHOD; 029 030/** 031 * {@code TestExecutionListener} which provides support for marking the 032 * {@code ApplicationContext} associated with a test as <em>dirty</em> for 033 * both test classes and test methods annotated with the 034 * {@link DirtiesContext @DirtiesContext} annotation. 035 * 036 * <p>This listener supports test methods with the 037 * {@linkplain DirtiesContext#methodMode method mode} set to 038 * {@link MethodMode#AFTER_METHOD AFTER_METHOD} and test classes with the 039 * {@linkplain DirtiesContext#classMode() class mode} set to 040 * {@link ClassMode#AFTER_EACH_TEST_METHOD AFTER_EACH_TEST_METHOD} or 041 * {@link ClassMode#AFTER_CLASS AFTER_CLASS}. For support for <em>BEFORE</em> 042 * modes, see {@link DirtiesContextBeforeModesTestExecutionListener}. 043 * 044 * <p>When {@linkplain TestExecutionListeners#mergeMode merging} 045 * {@code TestExecutionListeners} with the defaults, this listener will 046 * automatically be ordered after the {@link DependencyInjectionTestExecutionListener}; 047 * otherwise, this listener must be manually configured to execute after the 048 * {@code DependencyInjectionTestExecutionListener}. 049 * 050 * @author Sam Brannen 051 * @since 2.5 052 * @see DirtiesContext 053 * @see DirtiesContextBeforeModesTestExecutionListener 054 */ 055public class DirtiesContextTestExecutionListener extends AbstractDirtiesContextTestExecutionListener { 056 057 /** 058 * Returns {@code 3000}. 059 */ 060 @Override 061 public final int getOrder() { 062 return 3000; 063 } 064 065 /** 066 * If the current test method of the supplied {@linkplain TestContext test 067 * context} is annotated with {@code @DirtiesContext} and the {@linkplain 068 * DirtiesContext#methodMode() method mode} is set to {@link 069 * MethodMode#AFTER_METHOD AFTER_METHOD}, or if the test class is 070 * annotated with {@code @DirtiesContext} and the {@linkplain 071 * DirtiesContext#classMode() class mode} is set to {@link 072 * ClassMode#AFTER_EACH_TEST_METHOD AFTER_EACH_TEST_METHOD}, the 073 * {@linkplain ApplicationContext application context} of the test context 074 * will be {@linkplain TestContext#markApplicationContextDirty marked as dirty} and the 075 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE 076 * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to {@code true}. 077 */ 078 @Override 079 public void afterTestMethod(TestContext testContext) throws Exception { 080 beforeOrAfterTestMethod(testContext, AFTER_METHOD, AFTER_EACH_TEST_METHOD); 081 } 082 083 /** 084 * If the test class of the supplied {@linkplain TestContext test context} 085 * is annotated with {@code @DirtiesContext} and the {@linkplain 086 * DirtiesContext#classMode() class mode} is set to {@link 087 * ClassMode#AFTER_CLASS AFTER_CLASS}, the {@linkplain ApplicationContext 088 * application context} of the test context will be 089 * {@linkplain TestContext#markApplicationContextDirty marked as dirty}, and the 090 * {@link DependencyInjectionTestExecutionListener#REINJECT_DEPENDENCIES_ATTRIBUTE 091 * REINJECT_DEPENDENCIES_ATTRIBUTE} in the test context will be set to 092 * {@code true}. 093 */ 094 @Override 095 public void afterTestClass(TestContext testContext) throws Exception { 096 beforeOrAfterTestClass(testContext, AFTER_CLASS); 097 } 098 099}