|
| 1 | +/* |
| 2 | + * Copyright 2002-2019 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context.event; |
| 18 | + |
| 19 | +import org.springframework.test.context.TestContext; |
| 20 | +import org.springframework.test.context.support.AbstractTestExecutionListener; |
| 21 | + |
| 22 | +/** |
| 23 | + * {@link org.springframework.test.context.TestExecutionListener} that may be used to publish test life-cycle event to |
| 24 | + * the Spring test {@link org.springframework.context.ApplicationContext}. |
| 25 | + * |
| 26 | + * <p>These events may be consumed for various reasons, such as resetting {@em mock} beans or tracing test |
| 27 | + * execution. Since these events may consumed as part of regular Spring beans, they can be shared among |
| 28 | + * different test classes. |
| 29 | + * |
| 30 | + * <p>This {@link org.springframework.test.context.TestExecutionListener} is not active by default. Test classes |
| 31 | + * should be annotated using {@link org.springframework.test.context.TestExecutionListeners}, if they want to use it. |
| 32 | + * Alternatively, it may be added to {@code spring.factories}, if needed. |
| 33 | + * |
| 34 | + * @author Frank Scheffler |
| 35 | + * @since 5.2 |
| 36 | + * @see org.springframework.test.context.event.annotation.BeforeTestClass |
| 37 | + * @see org.springframework.test.context.event.annotation.PrepareTestInstance |
| 38 | + * @see org.springframework.test.context.event.annotation.BeforeTestMethod |
| 39 | + * @see org.springframework.test.context.event.annotation.BeforeTestExecution |
| 40 | + * @see org.springframework.test.context.event.annotation.AfterTestExecution |
| 41 | + * @see org.springframework.test.context.event.annotation.AfterTestMethod |
| 42 | + * @see org.springframework.test.context.event.annotation.AfterTestClass |
| 43 | + */ |
| 44 | +public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener { |
| 45 | + |
| 46 | + @Override |
| 47 | + public void beforeTestClass(TestContext testContext) { |
| 48 | + testContext.getApplicationContext().publishEvent( |
| 49 | + new BeforeTestClassEvent(testContext)); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void prepareTestInstance(TestContext testContext) { |
| 54 | + testContext.getApplicationContext().publishEvent( |
| 55 | + new PrepareTestInstanceEvent(testContext)); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void beforeTestMethod(TestContext testContext) { |
| 60 | + testContext.getApplicationContext().publishEvent( |
| 61 | + new BeforeTestMethodEvent(testContext)); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void beforeTestExecution(TestContext testContext) { |
| 66 | + testContext.getApplicationContext().publishEvent( |
| 67 | + new BeforeTestExecutionEvent(testContext)); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void afterTestExecution(TestContext testContext) { |
| 72 | + testContext.getApplicationContext().publishEvent( |
| 73 | + new AfterTestExecutionEvent(testContext)); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void afterTestMethod(TestContext testContext) { |
| 78 | + testContext.getApplicationContext().publishEvent( |
| 79 | + new AfterTestMethodEvent(testContext)); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void afterTestClass(TestContext testContext) { |
| 84 | + testContext.getApplicationContext().publishEvent( |
| 85 | + new AfterTestClassEvent(testContext)); |
| 86 | + } |
| 87 | +} |
0 commit comments