Skip to content

Commit 34fee86

Browse files
maverick1601sbrannen
authored andcommitted
Provide support for publishing TestContext lifecycle events
This commit introduces a new EventPublishingTestExecutionListener for publishing events to the test ApplicationContext. These may be consumed by @eventlistener annotated methods to react to the TestContext lifecycle. The listener is not registered by default. Closes gh-18490
1 parent a7db395 commit 34fee86

22 files changed

+1034
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
* TransactionalTestExecutionListener}</li>
5757
* <li>{@link org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
5858
* SqlScriptsTestExecutionListener}</li>
59+
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener
60+
* EventPublishingTestExecutionListener} (not registered by default)</li>
5961
* </ul>
6062
*
6163
* @author Sam Brannen

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

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
6767
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
6868
* @see org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener
69+
* @see org.springframework.test.context.event.EventPublishingTestExecutionListener
6970
*/
7071
@AliasFor("value")
7172
Class<? extends TestExecutionListener>[] listeners() default {};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
21+
/**
22+
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#afterTestClass(TestContext)} is called.
24+
*
25+
* @author Frank Scheffler
26+
* @since 5.2
27+
* @see org.springframework.test.context.event.annotation.AfterTestClass
28+
*/
29+
@SuppressWarnings("serial")
30+
public class AfterTestClassEvent extends TestContextEvent {
31+
32+
public AfterTestClassEvent(TestContext source) {
33+
super(source);
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
/**
22+
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#afterTestExecution(TestContext)} is called.
24+
*
25+
* @author Frank Scheffler
26+
* @since 5.2
27+
* @see org.springframework.test.context.event.annotation.AfterTestExecution
28+
*/
29+
@SuppressWarnings("serial")
30+
public class AfterTestExecutionEvent extends TestContextEvent {
31+
32+
public AfterTestExecutionEvent(TestContext source) {
33+
super(source);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
/**
22+
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#afterTestMethod(TestContext)} is called.
24+
*
25+
* @author Frank Scheffler
26+
* @since 5.2
27+
* @see org.springframework.test.context.event.annotation.AfterTestMethod
28+
*/
29+
@SuppressWarnings("serial")
30+
public class AfterTestMethodEvent extends TestContextEvent {
31+
32+
public AfterTestMethodEvent(TestContext source) {
33+
super(source);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
/**
22+
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#beforeTestClass(TestContext)} is called.
24+
*
25+
* @author Frank Scheffler
26+
* @since 5.2
27+
* @see org.springframework.test.context.event.annotation.BeforeTestClass
28+
*/
29+
@SuppressWarnings("serial")
30+
public class BeforeTestClassEvent extends TestContextEvent {
31+
32+
public BeforeTestClassEvent(TestContext source) {
33+
super(source);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
/**
22+
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution(TestContext)} is called.
24+
*
25+
* @author Frank Scheffler
26+
* @since 5.2
27+
* @see org.springframework.test.context.event.annotation.BeforeTestExecution
28+
*/
29+
@SuppressWarnings("serial")
30+
public class BeforeTestExecutionEvent extends TestContextEvent {
31+
32+
public BeforeTestExecutionEvent(TestContext source) {
33+
super(source);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
/**
22+
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)} is called.
24+
*
25+
* @author Frank Scheffler
26+
* @since 5.2
27+
* @see org.springframework.test.context.event.annotation.BeforeTestMethod
28+
*/
29+
@SuppressWarnings("serial")
30+
public class BeforeTestMethodEvent extends TestContextEvent {
31+
32+
public BeforeTestMethodEvent(TestContext source) {
33+
super(source);
34+
}
35+
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
21+
/**
22+
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when
23+
* {@link org.springframework.test.context.TestExecutionListener#prepareTestInstance(TestContext)} is called.
24+
*
25+
* @author Frank Scheffler
26+
* @since 5.2
27+
* @see org.springframework.test.context.event.annotation.PrepareTestInstance
28+
*/
29+
@SuppressWarnings("serial")
30+
public class PrepareTestInstanceEvent extends TestContextEvent {
31+
32+
public PrepareTestInstanceEvent(TestContext source) {
33+
super(source);
34+
}
35+
36+
}

0 commit comments

Comments
 (0)