-
Notifications
You must be signed in to change notification settings - Fork 38.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
a7db395
commit 34fee86
Showing
22 changed files
with
1,034 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
spring-test/src/main/java/org/springframework/test/context/event/AfterTestClassEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.event; | ||
|
||
import org.springframework.test.context.TestContext; | ||
|
||
/** | ||
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when | ||
* {@link org.springframework.test.context.TestExecutionListener#afterTestClass(TestContext)} is called. | ||
* | ||
* @author Frank Scheffler | ||
* @since 5.2 | ||
* @see org.springframework.test.context.event.annotation.AfterTestClass | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class AfterTestClassEvent extends TestContextEvent { | ||
|
||
public AfterTestClassEvent(TestContext source) { | ||
super(source); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...ng-test/src/main/java/org/springframework/test/context/event/AfterTestExecutionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.event; | ||
|
||
import org.springframework.test.context.TestContext; | ||
|
||
/** | ||
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when | ||
* {@link org.springframework.test.context.TestExecutionListener#afterTestExecution(TestContext)} is called. | ||
* | ||
* @author Frank Scheffler | ||
* @since 5.2 | ||
* @see org.springframework.test.context.event.annotation.AfterTestExecution | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class AfterTestExecutionEvent extends TestContextEvent { | ||
|
||
public AfterTestExecutionEvent(TestContext source) { | ||
super(source); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
spring-test/src/main/java/org/springframework/test/context/event/AfterTestMethodEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.event; | ||
|
||
import org.springframework.test.context.TestContext; | ||
|
||
/** | ||
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when | ||
* {@link org.springframework.test.context.TestExecutionListener#afterTestMethod(TestContext)} is called. | ||
* | ||
* @author Frank Scheffler | ||
* @since 5.2 | ||
* @see org.springframework.test.context.event.annotation.AfterTestMethod | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class AfterTestMethodEvent extends TestContextEvent { | ||
|
||
public AfterTestMethodEvent(TestContext source) { | ||
super(source); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
spring-test/src/main/java/org/springframework/test/context/event/BeforeTestClassEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.event; | ||
|
||
import org.springframework.test.context.TestContext; | ||
|
||
/** | ||
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when | ||
* {@link org.springframework.test.context.TestExecutionListener#beforeTestClass(TestContext)} is called. | ||
* | ||
* @author Frank Scheffler | ||
* @since 5.2 | ||
* @see org.springframework.test.context.event.annotation.BeforeTestClass | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class BeforeTestClassEvent extends TestContextEvent { | ||
|
||
public BeforeTestClassEvent(TestContext source) { | ||
super(source); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...g-test/src/main/java/org/springframework/test/context/event/BeforeTestExecutionEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.event; | ||
|
||
import org.springframework.test.context.TestContext; | ||
|
||
/** | ||
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when | ||
* {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution(TestContext)} is called. | ||
* | ||
* @author Frank Scheffler | ||
* @since 5.2 | ||
* @see org.springframework.test.context.event.annotation.BeforeTestExecution | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class BeforeTestExecutionEvent extends TestContextEvent { | ||
|
||
public BeforeTestExecutionEvent(TestContext source) { | ||
super(source); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
spring-test/src/main/java/org/springframework/test/context/event/BeforeTestMethodEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.event; | ||
|
||
import org.springframework.test.context.TestContext; | ||
|
||
/** | ||
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when | ||
* {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)} is called. | ||
* | ||
* @author Frank Scheffler | ||
* @since 5.2 | ||
* @see org.springframework.test.context.event.annotation.BeforeTestMethod | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class BeforeTestMethodEvent extends TestContextEvent { | ||
|
||
public BeforeTestMethodEvent(TestContext source) { | ||
super(source); | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
...ain/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.event; | ||
|
||
import org.springframework.test.context.TestContext; | ||
import org.springframework.test.context.support.AbstractTestExecutionListener; | ||
|
||
/** | ||
* {@link org.springframework.test.context.TestExecutionListener} that may be used to publish test life-cycle event to | ||
* the Spring test {@link org.springframework.context.ApplicationContext}. | ||
* | ||
* <p>These events may be consumed for various reasons, such as resetting {@em mock} beans or tracing test | ||
* execution. Since these events may consumed as part of regular Spring beans, they can be shared among | ||
* different test classes. | ||
* | ||
* <p>This {@link org.springframework.test.context.TestExecutionListener} is not active by default. Test classes | ||
* should be annotated using {@link org.springframework.test.context.TestExecutionListeners}, if they want to use it. | ||
* Alternatively, it may be added to {@code spring.factories}, if needed. | ||
* | ||
* @author Frank Scheffler | ||
* @since 5.2 | ||
* @see org.springframework.test.context.event.annotation.BeforeTestClass | ||
* @see org.springframework.test.context.event.annotation.PrepareTestInstance | ||
* @see org.springframework.test.context.event.annotation.BeforeTestMethod | ||
* @see org.springframework.test.context.event.annotation.BeforeTestExecution | ||
* @see org.springframework.test.context.event.annotation.AfterTestExecution | ||
* @see org.springframework.test.context.event.annotation.AfterTestMethod | ||
* @see org.springframework.test.context.event.annotation.AfterTestClass | ||
*/ | ||
public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener { | ||
|
||
@Override | ||
public void beforeTestClass(TestContext testContext) { | ||
testContext.getApplicationContext().publishEvent( | ||
new BeforeTestClassEvent(testContext)); | ||
} | ||
|
||
@Override | ||
public void prepareTestInstance(TestContext testContext) { | ||
testContext.getApplicationContext().publishEvent( | ||
new PrepareTestInstanceEvent(testContext)); | ||
} | ||
|
||
@Override | ||
public void beforeTestMethod(TestContext testContext) { | ||
testContext.getApplicationContext().publishEvent( | ||
new BeforeTestMethodEvent(testContext)); | ||
} | ||
|
||
@Override | ||
public void beforeTestExecution(TestContext testContext) { | ||
testContext.getApplicationContext().publishEvent( | ||
new BeforeTestExecutionEvent(testContext)); | ||
} | ||
|
||
@Override | ||
public void afterTestExecution(TestContext testContext) { | ||
testContext.getApplicationContext().publishEvent( | ||
new AfterTestExecutionEvent(testContext)); | ||
} | ||
|
||
@Override | ||
public void afterTestMethod(TestContext testContext) { | ||
testContext.getApplicationContext().publishEvent( | ||
new AfterTestMethodEvent(testContext)); | ||
} | ||
|
||
@Override | ||
public void afterTestClass(TestContext testContext) { | ||
testContext.getApplicationContext().publishEvent( | ||
new AfterTestClassEvent(testContext)); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...g-test/src/main/java/org/springframework/test/context/event/PrepareTestInstanceEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2002-2019 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.test.context.event; | ||
|
||
import org.springframework.test.context.TestContext; | ||
|
||
/** | ||
* {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when | ||
* {@link org.springframework.test.context.TestExecutionListener#prepareTestInstance(TestContext)} is called. | ||
* | ||
* @author Frank Scheffler | ||
* @since 5.2 | ||
* @see org.springframework.test.context.event.annotation.PrepareTestInstance | ||
*/ | ||
@SuppressWarnings("serial") | ||
public class PrepareTestInstanceEvent extends TestContextEvent { | ||
|
||
public PrepareTestInstanceEvent(TestContext source) { | ||
super(source); | ||
} | ||
|
||
} |
Oops, something went wrong.