|
16 | 16 |
|
17 | 17 | package org.springframework.test.context.event;
|
18 | 18 |
|
| 19 | +import org.springframework.core.Ordered; |
19 | 20 | import org.springframework.test.context.TestContext;
|
20 | 21 | import org.springframework.test.context.support.AbstractTestExecutionListener;
|
21 | 22 |
|
22 | 23 | /**
|
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}. |
| 24 | + * {@link org.springframework.test.context.TestExecutionListener TestExecutionListener} |
| 25 | + * that publishes test lifecycle events to a Spring test |
| 26 | + * {@link org.springframework.context.ApplicationContext ApplicationContext}. |
25 | 27 | *
|
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. |
| 28 | + * <p>These events may be consumed for various reasons, such as resetting <em>mock</em> |
| 29 | + * beans or tracing test execution. Since these events may be consumed by regular |
| 30 | + * Spring beans, they can be shared among different test classes. |
| 31 | + * |
| 32 | + * <h3>Supported Events</h3> |
| 33 | + * <ul> |
| 34 | + * <li>{@link BeforeTestClassEvent}</li> |
| 35 | + * <li>{@link PrepareTestInstanceEvent}</li> |
| 36 | + * <li>{@link BeforeTestMethodEvent}</li> |
| 37 | + * <li>{@link BeforeTestExecutionEvent}</li> |
| 38 | + * <li>{@link AfterTestExecutionEvent}</li> |
| 39 | + * <li>{@link AfterTestMethodEvent}</li> |
| 40 | + * <li>{@link AfterTestClassEvent}</li> |
| 41 | + * </ul> |
29 | 42 | *
|
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. |
| 43 | + * <p>Note that this {@code TestExecutionListener} is not registered by default, |
| 44 | + * but it may be registered for a given test class via |
| 45 | + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners} |
| 46 | + * or globally via the {@link org.springframework.core.io.support.SpringFactoriesLoader |
| 47 | + * SpringFactoriesLoader} mechanism (consult the Javadoc and Spring reference manual for |
| 48 | + * details). |
33 | 49 | *
|
34 | 50 | * @author Frank Scheffler
|
| 51 | + * @author Sam Brannen |
35 | 52 | * @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 |
| 53 | + * @see org.springframework.test.context.event.annotation.BeforeTestClass @BeforeTestClass |
| 54 | + * @see org.springframework.test.context.event.annotation.PrepareTestInstance @PrepareTestInstance |
| 55 | + * @see org.springframework.test.context.event.annotation.BeforeTestMethod @BeforeTestMethod |
| 56 | + * @see org.springframework.test.context.event.annotation.BeforeTestExecution @BeforeTestExecution |
| 57 | + * @see org.springframework.test.context.event.annotation.AfterTestExecution @AfterTestExecution |
| 58 | + * @see org.springframework.test.context.event.annotation.AfterTestMethod @AfterTestMethod |
| 59 | + * @see org.springframework.test.context.event.annotation.AfterTestClass @AfterTestClass |
43 | 60 | */
|
44 | 61 | public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener {
|
45 | 62 |
|
46 |
| - @Override |
47 |
| - public void beforeTestClass(TestContext testContext) { |
48 |
| - testContext.getApplicationContext().publishEvent( |
49 |
| - new BeforeTestClassEvent(testContext)); |
50 |
| - } |
| 63 | + /** |
| 64 | + * Returns {@link Ordered#HIGHEST_PRECEDENCE}. |
| 65 | + */ |
| 66 | + @Override |
| 67 | + public int getOrder() { |
| 68 | + return Ordered.HIGHEST_PRECEDENCE; |
| 69 | + } |
51 | 70 |
|
52 |
| - @Override |
53 |
| - public void prepareTestInstance(TestContext testContext) { |
54 |
| - testContext.getApplicationContext().publishEvent( |
55 |
| - new PrepareTestInstanceEvent(testContext)); |
56 |
| - } |
| 71 | + /** |
| 72 | + * Publishes a {@link BeforeTestClassEvent} to the {@code ApplicationContext} |
| 73 | + * for the supplied {@link TestContext}. |
| 74 | + */ |
| 75 | + @Override |
| 76 | + public void beforeTestClass(TestContext testContext) { |
| 77 | + testContext.getApplicationContext().publishEvent(new BeforeTestClassEvent(testContext)); |
| 78 | + } |
57 | 79 |
|
58 |
| - @Override |
59 |
| - public void beforeTestMethod(TestContext testContext) { |
60 |
| - testContext.getApplicationContext().publishEvent( |
61 |
| - new BeforeTestMethodEvent(testContext)); |
62 |
| - } |
| 80 | + /** |
| 81 | + * Publishes a {@link PrepareTestInstanceEvent} to the {@code ApplicationContext} |
| 82 | + * for the supplied {@link TestContext}. |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public void prepareTestInstance(TestContext testContext) { |
| 86 | + testContext.getApplicationContext().publishEvent(new PrepareTestInstanceEvent(testContext)); |
| 87 | + } |
63 | 88 |
|
64 |
| - @Override |
65 |
| - public void beforeTestExecution(TestContext testContext) { |
66 |
| - testContext.getApplicationContext().publishEvent( |
67 |
| - new BeforeTestExecutionEvent(testContext)); |
68 |
| - } |
| 89 | + /** |
| 90 | + * Publishes a {@link BeforeTestMethodEvent} to the {@code ApplicationContext} |
| 91 | + * for the supplied {@link TestContext}. |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public void beforeTestMethod(TestContext testContext) { |
| 95 | + testContext.getApplicationContext().publishEvent(new BeforeTestMethodEvent(testContext)); |
| 96 | + } |
69 | 97 |
|
70 |
| - @Override |
71 |
| - public void afterTestExecution(TestContext testContext) { |
72 |
| - testContext.getApplicationContext().publishEvent( |
73 |
| - new AfterTestExecutionEvent(testContext)); |
74 |
| - } |
| 98 | + /** |
| 99 | + * Publishes a {@link BeforeTestExecutionEvent} to the {@code ApplicationContext} |
| 100 | + * for the supplied {@link TestContext}. |
| 101 | + */ |
| 102 | + @Override |
| 103 | + public void beforeTestExecution(TestContext testContext) { |
| 104 | + testContext.getApplicationContext().publishEvent(new BeforeTestExecutionEvent(testContext)); |
| 105 | + } |
75 | 106 |
|
76 |
| - @Override |
77 |
| - public void afterTestMethod(TestContext testContext) { |
78 |
| - testContext.getApplicationContext().publishEvent( |
79 |
| - new AfterTestMethodEvent(testContext)); |
80 |
| - } |
| 107 | + /** |
| 108 | + * Publishes an {@link AfterTestExecutionEvent} to the {@code ApplicationContext} |
| 109 | + * for the supplied {@link TestContext}. |
| 110 | + */ |
| 111 | + @Override |
| 112 | + public void afterTestExecution(TestContext testContext) { |
| 113 | + testContext.getApplicationContext().publishEvent(new AfterTestExecutionEvent(testContext)); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Publishes an {@link AfterTestMethodEvent} to the {@code ApplicationContext} |
| 118 | + * for the supplied {@link TestContext}. |
| 119 | + */ |
| 120 | + @Override |
| 121 | + public void afterTestMethod(TestContext testContext) { |
| 122 | + testContext.getApplicationContext().publishEvent(new AfterTestMethodEvent(testContext)); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Publishes an {@link AfterTestClassEvent} to the {@code ApplicationContext} |
| 127 | + * for the supplied {@link TestContext}. |
| 128 | + */ |
| 129 | + @Override |
| 130 | + public void afterTestClass(TestContext testContext) { |
| 131 | + testContext.getApplicationContext().publishEvent(new AfterTestClassEvent(testContext)); |
| 132 | + } |
81 | 133 |
|
82 |
| - @Override |
83 |
| - public void afterTestClass(TestContext testContext) { |
84 |
| - testContext.getApplicationContext().publishEvent( |
85 |
| - new AfterTestClassEvent(testContext)); |
86 |
| - } |
87 | 134 | }
|
0 commit comments