Skip to content

Commit 2d6624d

Browse files
committed
Polish support for publishing TestContext lifecycle events
See gh-18490
1 parent 34fee86 commit 2d6624d

20 files changed

+459
-354
lines changed

spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
* <p>Spring provides the following out-of-the-box implementations (all of
4545
* which implement {@code Ordered}):
4646
* <ul>
47+
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener
48+
* EventPublishingTestExecutionListener} (not registered by default)</li>
4749
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener
4850
* ServletTestExecutionListener}</li>
4951
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
@@ -56,13 +58,12 @@
5658
* TransactionalTestExecutionListener}</li>
5759
* <li>{@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
5860
* SqlScriptsTestExecutionListener}</li>
59-
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener
60-
* EventPublishingTestExecutionListener} (not registered by default)</li>
6161
* </ul>
6262
*
6363
* @author Sam Brannen
6464
* @author Juergen Hoeller
6565
* @since 2.5
66+
* @see TestExecutionListeners @TestExecutionListeners
6667
* @see TestContextManager
6768
* @see org.springframework.test.context.support.AbstractTestExecutionListener
6869
*/

spring-test/src/main/java/org/springframework/test/context/TestExecutionListeners.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@
6161
* the {@link TestContextManager}.
6262
* <p>This attribute may <strong>not</strong> be used in conjunction with
6363
* {@link #value}, but it may be used instead of {@link #value}.
64+
* @see org.springframework.test.context.event.EventPublishingTestExecutionListener
6465
* @see org.springframework.test.context.web.ServletTestExecutionListener
66+
* @see org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
6567
* @see org.springframework.test.context.support.DependencyInjectionTestExecutionListener
6668
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
6769
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
6870
* @see org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
69-
* @see org.springframework.test.context.event.EventPublishingTestExecutionListener
7071
*/
7172
@AliasFor("value")
7273
Class<? extends TestExecutionListener>[] listeners() default {};

spring-test/src/main/java/org/springframework/test/context/event/AfterTestClassEvent.java

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@
1919
import org.springframework.test.context.TestContext;
2020

2121
/**
22-
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23-
* {@link org.springframework.test.context.TestExecutionListener#afterTestClass(TestContext)} is called.
22+
* {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#afterTestClass(TestContext)}
24+
* is invoked.
2425
*
2526
* @author Frank Scheffler
2627
* @since 5.2
27-
* @see org.springframework.test.context.event.annotation.AfterTestClass
28+
* @see org.springframework.test.context.event.annotation.AfterTestClass @AfterTestClass
2829
*/
2930
@SuppressWarnings("serial")
3031
public class AfterTestClassEvent extends TestContextEvent {
3132

32-
public AfterTestClassEvent(TestContext source) {
33-
super(source);
34-
}
33+
public AfterTestClassEvent(TestContext source) {
34+
super(source);
35+
}
36+
3537
}

spring-test/src/main/java/org/springframework/test/context/event/AfterTestExecutionEvent.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
import org.springframework.test.context.TestContext;
2020

2121
/**
22-
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23-
* {@link org.springframework.test.context.TestExecutionListener#afterTestExecution(TestContext)} is called.
22+
* {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#afterTestExecution(TestContext)}
24+
* is invoked.
2425
*
2526
* @author Frank Scheffler
2627
* @since 5.2
27-
* @see org.springframework.test.context.event.annotation.AfterTestExecution
28+
* @see org.springframework.test.context.event.annotation.AfterTestExecution @AfterTestExecution
2829
*/
2930
@SuppressWarnings("serial")
3031
public class AfterTestExecutionEvent extends TestContextEvent {
3132

32-
public AfterTestExecutionEvent(TestContext source) {
33-
super(source);
34-
}
33+
public AfterTestExecutionEvent(TestContext source) {
34+
super(source);
35+
}
3536

3637
}

spring-test/src/main/java/org/springframework/test/context/event/AfterTestMethodEvent.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
import org.springframework.test.context.TestContext;
2020

2121
/**
22-
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23-
* {@link org.springframework.test.context.TestExecutionListener#afterTestMethod(TestContext)} is called.
22+
* {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#afterTestMethod(TestContext)}
24+
* is invoked.
2425
*
2526
* @author Frank Scheffler
2627
* @since 5.2
27-
* @see org.springframework.test.context.event.annotation.AfterTestMethod
28+
* @see org.springframework.test.context.event.annotation.AfterTestMethod @AfterTestMethod
2829
*/
2930
@SuppressWarnings("serial")
3031
public class AfterTestMethodEvent extends TestContextEvent {
3132

32-
public AfterTestMethodEvent(TestContext source) {
33-
super(source);
34-
}
33+
public AfterTestMethodEvent(TestContext source) {
34+
super(source);
35+
}
3536

3637
}

spring-test/src/main/java/org/springframework/test/context/event/BeforeTestClassEvent.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
import org.springframework.test.context.TestContext;
2020

2121
/**
22-
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23-
* {@link org.springframework.test.context.TestExecutionListener#beforeTestClass(TestContext)} is called.
22+
* {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#beforeTestClass(TestContext)}
24+
* is invoked.
2425
*
2526
* @author Frank Scheffler
2627
* @since 5.2
27-
* @see org.springframework.test.context.event.annotation.BeforeTestClass
28+
* @see org.springframework.test.context.event.annotation.BeforeTestClass @BeforeTestClass
2829
*/
2930
@SuppressWarnings("serial")
3031
public class BeforeTestClassEvent extends TestContextEvent {
3132

32-
public BeforeTestClassEvent(TestContext source) {
33-
super(source);
34-
}
33+
public BeforeTestClassEvent(TestContext source) {
34+
super(source);
35+
}
3536

3637
}

spring-test/src/main/java/org/springframework/test/context/event/BeforeTestExecutionEvent.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
import org.springframework.test.context.TestContext;
2020

2121
/**
22-
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23-
* {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution(TestContext)} is called.
22+
* {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution(TestContext)}
24+
* is invoked.
2425
*
2526
* @author Frank Scheffler
2627
* @since 5.2
27-
* @see org.springframework.test.context.event.annotation.BeforeTestExecution
28+
* @see org.springframework.test.context.event.annotation.BeforeTestExecution @BeforeTestExecution
2829
*/
2930
@SuppressWarnings("serial")
3031
public class BeforeTestExecutionEvent extends TestContextEvent {
3132

32-
public BeforeTestExecutionEvent(TestContext source) {
33-
super(source);
34-
}
33+
public BeforeTestExecutionEvent(TestContext source) {
34+
super(source);
35+
}
3536

3637
}

spring-test/src/main/java/org/springframework/test/context/event/BeforeTestMethodEvent.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
import org.springframework.test.context.TestContext;
2020

2121
/**
22-
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23-
* {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)} is called.
22+
* {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)}
24+
* is invoked.
2425
*
2526
* @author Frank Scheffler
2627
* @since 5.2
27-
* @see org.springframework.test.context.event.annotation.BeforeTestMethod
28+
* @see org.springframework.test.context.event.annotation.BeforeTestMethod @BeforeTestMethod
2829
*/
2930
@SuppressWarnings("serial")
3031
public class BeforeTestMethodEvent extends TestContextEvent {
3132

32-
public BeforeTestMethodEvent(TestContext source) {
33-
super(source);
34-
}
33+
public BeforeTestMethodEvent(TestContext source) {
34+
super(source);
35+
}
3536

3637
}

spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java

+97-50
Original file line numberDiff line numberDiff line change
@@ -16,72 +16,119 @@
1616

1717
package org.springframework.test.context.event;
1818

19+
import org.springframework.core.Ordered;
1920
import org.springframework.test.context.TestContext;
2021
import org.springframework.test.context.support.AbstractTestExecutionListener;
2122

2223
/**
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}.
2527
*
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>
2942
*
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).
3349
*
3450
* @author Frank Scheffler
51+
* @author Sam Brannen
3552
* @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
4360
*/
4461
public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener {
4562

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+
}
5170

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+
}
5779

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+
}
6388

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+
}
6997

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+
}
75106

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+
}
81133

82-
@Override
83-
public void afterTestClass(TestContext testContext) {
84-
testContext.getApplicationContext().publishEvent(
85-
new AfterTestClassEvent(testContext));
86-
}
87134
}

spring-test/src/main/java/org/springframework/test/context/event/PrepareTestInstanceEvent.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,19 @@
1919
import org.springframework.test.context.TestContext;
2020

2121
/**
22-
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23-
* {@link org.springframework.test.context.TestExecutionListener#prepareTestInstance(TestContext)} is called.
22+
* {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#prepareTestInstance(TestContext)}
24+
* is invoked.
2425
*
2526
* @author Frank Scheffler
2627
* @since 5.2
27-
* @see org.springframework.test.context.event.annotation.PrepareTestInstance
28+
* @see org.springframework.test.context.event.annotation.PrepareTestInstance @PrepareTestInstance
2829
*/
2930
@SuppressWarnings("serial")
3031
public class PrepareTestInstanceEvent extends TestContextEvent {
3132

32-
public PrepareTestInstanceEvent(TestContext source) {
33-
super(source);
34-
}
33+
public PrepareTestInstanceEvent(TestContext source) {
34+
super(source);
35+
}
3536

3637
}

0 commit comments

Comments
 (0)