001/* 002 * Copyright 2002-2016 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; 018 019/** 020 * {@code TestExecutionListener} defines a <em>listener</em> API for reacting to 021 * test execution events published by the {@link TestContextManager} with which 022 * the listener is registered. 023 * 024 * <p>Concrete implementations must provide a {@code public} no-args constructor, 025 * so that listeners can be instantiated transparently by tools and configuration 026 * mechanisms. 027 * 028 * <p>Implementations may optionally declare the position in which they should 029 * be ordered among the chain of default listeners via the 030 * {@link org.springframework.core.Ordered Ordered} interface or 031 * {@link org.springframework.core.annotation.Order @Order} annotation. See 032 * {@link TestContextBootstrapper#getTestExecutionListeners()} for details. 033 * 034 * <p>Spring provides the following out-of-the-box implementations (all of 035 * which implement {@code Ordered}): 036 * <ul> 037 * <li>{@link org.springframework.test.context.web.ServletTestExecutionListener 038 * ServletTestExecutionListener}</li> 039 * <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener 040 * DirtiesContextBeforeModesTestExecutionListener}</li> 041 * <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener 042 * DependencyInjectionTestExecutionListener}</li> 043 * <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener 044 * DirtiesContextTestExecutionListener}</li> 045 * <li>{@link org.springframework.test.context.transaction.TransactionalTestExecutionListener 046 * TransactionalTestExecutionListener}</li> 047 * <li>{@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener 048 * SqlScriptsTestExecutionListener}</li> 049 * </ul> 050 * 051 * @author Sam Brannen 052 * @author Juergen Hoeller 053 * @since 2.5 054 */ 055public interface TestExecutionListener { 056 057 /** 058 * Pre-processes a test class <em>before</em> execution of all tests within 059 * the class. 060 * <p>This method should be called immediately before framework-specific 061 * <em>before class</em> lifecycle callbacks. 062 * <p>If a given testing framework does not support <em>before class</em> 063 * lifecycle callbacks, this method will not be called for that framework. 064 * @param testContext the test context for the test; never {@code null} 065 * @throws Exception allows any exception to propagate 066 */ 067 void beforeTestClass(TestContext testContext) throws Exception; 068 069 /** 070 * Prepares the {@link Object test instance} of the supplied 071 * {@link TestContext test context}, for example by injecting dependencies. 072 * <p>This method should be called immediately after instantiation of the test 073 * instance but prior to any framework-specific lifecycle callbacks. 074 * @param testContext the test context for the test; never {@code null} 075 * @throws Exception allows any exception to propagate 076 */ 077 void prepareTestInstance(TestContext testContext) throws Exception; 078 079 /** 080 * Pre-processes a test <em>before</em> execution of the 081 * {@link java.lang.reflect.Method test method} in the supplied 082 * {@link TestContext test context}, for example by setting up test 083 * fixtures. 084 * <p>This method should be called immediately prior to framework-specific 085 * <em>before</em> lifecycle callbacks. 086 * @param testContext the test context in which the test method will be 087 * executed; never {@code null} 088 * @throws Exception allows any exception to propagate 089 */ 090 void beforeTestMethod(TestContext testContext) throws Exception; 091 092 /** 093 * Post-processes a test <em>after</em> execution of the 094 * {@link java.lang.reflect.Method test method} in the supplied 095 * {@link TestContext test context}, for example by tearing down test 096 * fixtures. 097 * <p>This method should be called immediately after framework-specific 098 * <em>after</em> lifecycle callbacks. 099 * @param testContext the test context in which the test method was 100 * executed; never {@code null} 101 * @throws Exception allows any exception to propagate 102 */ 103 void afterTestMethod(TestContext testContext) throws Exception; 104 105 /** 106 * Post-processes a test class <em>after</em> execution of all tests within 107 * the class. 108 * <p>This method should be called immediately after framework-specific 109 * <em>after class</em> lifecycle callbacks. 110 * <p>If a given testing framework does not support <em>after class</em> 111 * lifecycle callbacks, this method will not be called for that framework. 112 * @param testContext the test context for the test; never {@code null} 113 * @throws Exception allows any exception to propagate 114 */ 115 void afterTestClass(TestContext testContext) throws Exception; 116 117}