diff --git a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java index 8cae398efea1..7189c81a17c5 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListener.java @@ -44,6 +44,8 @@ *

Spring provides the following out-of-the-box implementations (all of * which implement {@code Ordered}): *

* * @author Sam Brannen * @author Juergen Hoeller * @since 2.5 + * @see TestExecutionListeners @TestExecutionListeners * @see TestContextManager * @see org.springframework.test.context.support.AbstractTestExecutionListener */ diff --git a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListeners.java b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListeners.java index 1dcec5e8c0e9..d2143db81358 100644 --- a/spring-test/src/main/java/org/springframework/test/context/TestExecutionListeners.java +++ b/spring-test/src/main/java/org/springframework/test/context/TestExecutionListeners.java @@ -61,12 +61,13 @@ * the {@link TestContextManager}. *

This attribute may not be used in conjunction with * {@link #value}, but it may be used instead of {@link #value}. + * @see org.springframework.test.context.event.EventPublishingTestExecutionListener * @see org.springframework.test.context.web.ServletTestExecutionListener + * @see org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener * @see org.springframework.test.context.support.DependencyInjectionTestExecutionListener * @see org.springframework.test.context.support.DirtiesContextTestExecutionListener * @see org.springframework.test.context.transaction.TransactionalTestExecutionListener * @see org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener - * @see org.springframework.test.context.event.EventPublishingTestExecutionListener */ @AliasFor("value") Class[] listeners() default {}; diff --git a/spring-test/src/main/java/org/springframework/test/context/event/AfterTestClassEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/AfterTestClassEvent.java index c54f00e26fdc..a86c312e36f2 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/AfterTestClassEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/AfterTestClassEvent.java @@ -19,17 +19,19 @@ import org.springframework.test.context.TestContext; /** - * {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when - * {@link org.springframework.test.context.TestExecutionListener#afterTestClass(TestContext)} is called. + * {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when + * {@link org.springframework.test.context.TestExecutionListener#afterTestClass(TestContext)} + * is invoked. * * @author Frank Scheffler * @since 5.2 - * @see org.springframework.test.context.event.annotation.AfterTestClass + * @see org.springframework.test.context.event.annotation.AfterTestClass @AfterTestClass */ @SuppressWarnings("serial") public class AfterTestClassEvent extends TestContextEvent { - public AfterTestClassEvent(TestContext source) { - super(source); - } + public AfterTestClassEvent(TestContext source) { + super(source); + } + } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/AfterTestExecutionEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/AfterTestExecutionEvent.java index b103bab17d54..be89fb24e767 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/AfterTestExecutionEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/AfterTestExecutionEvent.java @@ -19,18 +19,19 @@ import org.springframework.test.context.TestContext; /** - * {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when - * {@link org.springframework.test.context.TestExecutionListener#afterTestExecution(TestContext)} is called. + * {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when + * {@link org.springframework.test.context.TestExecutionListener#afterTestExecution(TestContext)} + * is invoked. * * @author Frank Scheffler * @since 5.2 - * @see org.springframework.test.context.event.annotation.AfterTestExecution + * @see org.springframework.test.context.event.annotation.AfterTestExecution @AfterTestExecution */ @SuppressWarnings("serial") public class AfterTestExecutionEvent extends TestContextEvent { - public AfterTestExecutionEvent(TestContext source) { - super(source); - } + public AfterTestExecutionEvent(TestContext source) { + super(source); + } } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/AfterTestMethodEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/AfterTestMethodEvent.java index b44eada444fa..36e72586a586 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/AfterTestMethodEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/AfterTestMethodEvent.java @@ -19,18 +19,19 @@ import org.springframework.test.context.TestContext; /** - * {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when - * {@link org.springframework.test.context.TestExecutionListener#afterTestMethod(TestContext)} is called. + * {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when + * {@link org.springframework.test.context.TestExecutionListener#afterTestMethod(TestContext)} + * is invoked. * * @author Frank Scheffler * @since 5.2 - * @see org.springframework.test.context.event.annotation.AfterTestMethod + * @see org.springframework.test.context.event.annotation.AfterTestMethod @AfterTestMethod */ @SuppressWarnings("serial") public class AfterTestMethodEvent extends TestContextEvent { - public AfterTestMethodEvent(TestContext source) { - super(source); - } + public AfterTestMethodEvent(TestContext source) { + super(source); + } } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestClassEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestClassEvent.java index 760af2a090cf..550e743dd9c3 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestClassEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestClassEvent.java @@ -19,18 +19,19 @@ import org.springframework.test.context.TestContext; /** - * {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when - * {@link org.springframework.test.context.TestExecutionListener#beforeTestClass(TestContext)} is called. + * {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when + * {@link org.springframework.test.context.TestExecutionListener#beforeTestClass(TestContext)} + * is invoked. * * @author Frank Scheffler * @since 5.2 - * @see org.springframework.test.context.event.annotation.BeforeTestClass + * @see org.springframework.test.context.event.annotation.BeforeTestClass @BeforeTestClass */ @SuppressWarnings("serial") public class BeforeTestClassEvent extends TestContextEvent { - public BeforeTestClassEvent(TestContext source) { - super(source); - } + public BeforeTestClassEvent(TestContext source) { + super(source); + } } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestExecutionEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestExecutionEvent.java index 99907e063150..851122bf62a9 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestExecutionEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestExecutionEvent.java @@ -19,18 +19,19 @@ import org.springframework.test.context.TestContext; /** - * {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when - * {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution(TestContext)} is called. + * {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when + * {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution(TestContext)} + * is invoked. * * @author Frank Scheffler * @since 5.2 - * @see org.springframework.test.context.event.annotation.BeforeTestExecution + * @see org.springframework.test.context.event.annotation.BeforeTestExecution @BeforeTestExecution */ @SuppressWarnings("serial") public class BeforeTestExecutionEvent extends TestContextEvent { - public BeforeTestExecutionEvent(TestContext source) { - super(source); - } + public BeforeTestExecutionEvent(TestContext source) { + super(source); + } } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestMethodEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestMethodEvent.java index ab1c0ed972e2..42f3d1bf3e56 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestMethodEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/BeforeTestMethodEvent.java @@ -19,18 +19,19 @@ import org.springframework.test.context.TestContext; /** - * {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when - * {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)} is called. + * {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when + * {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(TestContext)} + * is invoked. * * @author Frank Scheffler * @since 5.2 - * @see org.springframework.test.context.event.annotation.BeforeTestMethod + * @see org.springframework.test.context.event.annotation.BeforeTestMethod @BeforeTestMethod */ @SuppressWarnings("serial") public class BeforeTestMethodEvent extends TestContextEvent { - public BeforeTestMethodEvent(TestContext source) { - super(source); - } + public BeforeTestMethodEvent(TestContext source) { + super(source); + } } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java b/spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java index 6d55db6bafcf..0e93a195e5cd 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/EventPublishingTestExecutionListener.java @@ -16,72 +16,119 @@ package org.springframework.test.context.event; +import org.springframework.core.Ordered; 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}. + * {@link org.springframework.test.context.TestExecutionListener TestExecutionListener} + * that publishes test lifecycle events to a Spring test + * {@link org.springframework.context.ApplicationContext ApplicationContext}. * - *

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. + *

These events may be consumed for various reasons, such as resetting mock + * beans or tracing test execution. Since these events may be consumed by regular + * Spring beans, they can be shared among different test classes. + * + *

Supported Events

+ * * - *

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. + *

Note that this {@code TestExecutionListener} is not registered by default, + * but it may be registered for a given test class via + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners} + * or globally via the {@link org.springframework.core.io.support.SpringFactoriesLoader + * SpringFactoriesLoader} mechanism (consult the Javadoc and Spring reference manual for + * details). * * @author Frank Scheffler + * @author Sam Brannen * @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 + * @see org.springframework.test.context.event.annotation.BeforeTestClass @BeforeTestClass + * @see org.springframework.test.context.event.annotation.PrepareTestInstance @PrepareTestInstance + * @see org.springframework.test.context.event.annotation.BeforeTestMethod @BeforeTestMethod + * @see org.springframework.test.context.event.annotation.BeforeTestExecution @BeforeTestExecution + * @see org.springframework.test.context.event.annotation.AfterTestExecution @AfterTestExecution + * @see org.springframework.test.context.event.annotation.AfterTestMethod @AfterTestMethod + * @see org.springframework.test.context.event.annotation.AfterTestClass @AfterTestClass */ public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener { - @Override - public void beforeTestClass(TestContext testContext) { - testContext.getApplicationContext().publishEvent( - new BeforeTestClassEvent(testContext)); - } + /** + * Returns {@link Ordered#HIGHEST_PRECEDENCE}. + */ + @Override + public int getOrder() { + return Ordered.HIGHEST_PRECEDENCE; + } - @Override - public void prepareTestInstance(TestContext testContext) { - testContext.getApplicationContext().publishEvent( - new PrepareTestInstanceEvent(testContext)); - } + /** + * Publishes a {@link BeforeTestClassEvent} to the {@code ApplicationContext} + * for the supplied {@link TestContext}. + */ + @Override + public void beforeTestClass(TestContext testContext) { + testContext.getApplicationContext().publishEvent(new BeforeTestClassEvent(testContext)); + } - @Override - public void beforeTestMethod(TestContext testContext) { - testContext.getApplicationContext().publishEvent( - new BeforeTestMethodEvent(testContext)); - } + /** + * Publishes a {@link PrepareTestInstanceEvent} to the {@code ApplicationContext} + * for the supplied {@link TestContext}. + */ + @Override + public void prepareTestInstance(TestContext testContext) { + testContext.getApplicationContext().publishEvent(new PrepareTestInstanceEvent(testContext)); + } - @Override - public void beforeTestExecution(TestContext testContext) { - testContext.getApplicationContext().publishEvent( - new BeforeTestExecutionEvent(testContext)); - } + /** + * Publishes a {@link BeforeTestMethodEvent} to the {@code ApplicationContext} + * for the supplied {@link TestContext}. + */ + @Override + public void beforeTestMethod(TestContext testContext) { + testContext.getApplicationContext().publishEvent(new BeforeTestMethodEvent(testContext)); + } - @Override - public void afterTestExecution(TestContext testContext) { - testContext.getApplicationContext().publishEvent( - new AfterTestExecutionEvent(testContext)); - } + /** + * Publishes a {@link BeforeTestExecutionEvent} to the {@code ApplicationContext} + * for the supplied {@link TestContext}. + */ + @Override + public void beforeTestExecution(TestContext testContext) { + testContext.getApplicationContext().publishEvent(new BeforeTestExecutionEvent(testContext)); + } - @Override - public void afterTestMethod(TestContext testContext) { - testContext.getApplicationContext().publishEvent( - new AfterTestMethodEvent(testContext)); - } + /** + * Publishes an {@link AfterTestExecutionEvent} to the {@code ApplicationContext} + * for the supplied {@link TestContext}. + */ + @Override + public void afterTestExecution(TestContext testContext) { + testContext.getApplicationContext().publishEvent(new AfterTestExecutionEvent(testContext)); + } + + /** + * Publishes an {@link AfterTestMethodEvent} to the {@code ApplicationContext} + * for the supplied {@link TestContext}. + */ + @Override + public void afterTestMethod(TestContext testContext) { + testContext.getApplicationContext().publishEvent(new AfterTestMethodEvent(testContext)); + } + + /** + * Publishes an {@link AfterTestClassEvent} to the {@code ApplicationContext} + * for the supplied {@link TestContext}. + */ + @Override + public void afterTestClass(TestContext testContext) { + testContext.getApplicationContext().publishEvent(new AfterTestClassEvent(testContext)); + } - @Override - public void afterTestClass(TestContext testContext) { - testContext.getApplicationContext().publishEvent( - new AfterTestClassEvent(testContext)); - } } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/PrepareTestInstanceEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/PrepareTestInstanceEvent.java index 5a26fb80008b..6068e0e6702e 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/PrepareTestInstanceEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/PrepareTestInstanceEvent.java @@ -19,18 +19,19 @@ import org.springframework.test.context.TestContext; /** - * {@link TestContextEvent} published by {@link EventPublishingTestExecutionListener} when - * {@link org.springframework.test.context.TestExecutionListener#prepareTestInstance(TestContext)} is called. + * {@link TestContextEvent} published by the {@link EventPublishingTestExecutionListener} when + * {@link org.springframework.test.context.TestExecutionListener#prepareTestInstance(TestContext)} + * is invoked. * * @author Frank Scheffler * @since 5.2 - * @see org.springframework.test.context.event.annotation.PrepareTestInstance + * @see org.springframework.test.context.event.annotation.PrepareTestInstance @PrepareTestInstance */ @SuppressWarnings("serial") public class PrepareTestInstanceEvent extends TestContextEvent { - public PrepareTestInstanceEvent(TestContext source) { - super(source); - } + public PrepareTestInstanceEvent(TestContext source) { + super(source); + } } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/TestContextEvent.java b/spring-test/src/main/java/org/springframework/test/context/event/TestContextEvent.java index 20a3787b8942..7a1bfb0d9345 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/TestContextEvent.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/TestContextEvent.java @@ -20,25 +20,31 @@ import org.springframework.test.context.TestContext; /** - * Base class for events published by {@link EventPublishingTestExecutionListener}. + * Base class for events published by the {@link EventPublishingTestExecutionListener}. * * @author Frank Scheffler + * @author Sam Brannen * @since 5.2 */ @SuppressWarnings("serial") public abstract class TestContextEvent extends ApplicationEvent { - public TestContextEvent(TestContext source) { - super(source); - } + /** + * Create a new {@code TestContextEvent}. + * @param source the {@code TestContext} associated with this event + * (must not be {@code null}) + */ + public TestContextEvent(TestContext source) { + super(source); + } + + /** + * Get the {@code TestContext} associated with this event. + * @return the {@code TestContext} associated with this event (never {@code null}) + */ + @Override + public TestContext getSource() { + return (TestContext) super.getSource(); + } - /* - * (non-Javadoc) - * - * @see java.util.EventObject#getSource() - */ - @Override - public TestContext getSource() { - return (TestContext) super.getSource(); - } } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestClass.java b/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestClass.java index b4e7ddb6fcfc..349446f48736 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestClass.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestClass.java @@ -16,30 +16,34 @@ package org.springframework.test.context.event.annotation; -import org.springframework.context.event.EventListener; -import org.springframework.test.context.event.AfterTestClassEvent; - import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; +import org.springframework.context.event.EventListener; +import org.springframework.test.context.event.AfterTestClassEvent; + import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * {@link EventListener} annotation used to consume {@link AfterTestClassEvent}s published - * by {@link org.springframework.test.context.event.EventPublishingTestExecutionListener}. + * {@link EventListener @EventListener} annotation used to consume a + * {@link AfterTestClassEvent} published by the + * {@link org.springframework.test.context.event.EventPublishingTestExecutionListener + * EventPublishingTestExecutionListener}. * - *

This annotation may be used on {@link EventListener}-compliant methods within the Spring test - * {@link org.springframework.context.ApplicationContext}, typically within - * {@link org.springframework.context.annotation.Configuration}s. A method annotated hereby will be - * called as part of the {@link org.springframework.test.context.TestExecutionListener#afterTestClass(org.springframework.test.context.TestContext)} - * life-cycle method. + *

This annotation may be used on {@code @EventListener}-compliant methods within + * a Spring test {@link org.springframework.context.ApplicationContext ApplicationContext} + * — for example, on methods in a + * {@link org.springframework.context.annotation.Configuration @Configuration} + * class. A method annotated with this annotation will be invoked as part of the + * {@link org.springframework.test.context.TestExecutionListener#afterTestClass} + * lifecycle. * - *

Make sure {@link org.springframework.test.context.event.EventPublishingTestExecutionListener} is enabled, - * for this annotation to have an effect, e.g. by annotation your test class with - * {@link org.springframework.test.context.TestExecutionListeners} accordingly. + *

The {@code EventPublishingTestExecutionListener} must be registered in order + * for this annotation to have an effect — for example, via + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}. * * @author Frank Scheffler * @since 5.2 @@ -47,7 +51,7 @@ */ @Documented @Retention(RUNTIME) -@Target({METHOD, ANNOTATION_TYPE}) +@Target({ METHOD, ANNOTATION_TYPE }) @EventListener(AfterTestClassEvent.class) public @interface AfterTestClass { } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestExecution.java b/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestExecution.java index 642699d70cd1..946a73a81172 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestExecution.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestExecution.java @@ -16,30 +16,34 @@ package org.springframework.test.context.event.annotation; -import org.springframework.context.event.EventListener; -import org.springframework.test.context.event.AfterTestExecutionEvent; - import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; +import org.springframework.context.event.EventListener; +import org.springframework.test.context.event.AfterTestExecutionEvent; + import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * {@link EventListener} annotation used to consume {@link AfterTestExecutionEvent}s published - * by {@link org.springframework.test.context.event.EventPublishingTestExecutionListener}. + * {@link EventListener @EventListener} annotation used to consume a + * {@link AfterTestExecutionEvent} published by the + * {@link org.springframework.test.context.event.EventPublishingTestExecutionListener + * EventPublishingTestExecutionListener}. * - *

This annotation may be used on {@link EventListener}-compliant methods within the Spring test - * {@link org.springframework.context.ApplicationContext}, typically within - * {@link org.springframework.context.annotation.Configuration}s. A method annotated hereby will be - * called as part of the {@link org.springframework.test.context.TestExecutionListener#afterTestExecution(org.springframework.test.context.TestContext)} - * life-cycle method. + *

This annotation may be used on {@code @EventListener}-compliant methods within + * a Spring test {@link org.springframework.context.ApplicationContext ApplicationContext} + * — for example, on methods in a + * {@link org.springframework.context.annotation.Configuration @Configuration} + * class. A method annotated with this annotation will be invoked as part of the + * {@link org.springframework.test.context.TestExecutionListener#afterTestExecution} + * lifecycle. * - *

Make sure {@link org.springframework.test.context.event.EventPublishingTestExecutionListener} is enabled, - * for this annotation to have an effect, e.g. by annotation your test class with - * {@link org.springframework.test.context.TestExecutionListeners} accordingly. + *

The {@code EventPublishingTestExecutionListener} must be registered in order + * for this annotation to have an effect — for example, via + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}. * * @author Frank Scheffler * @since 5.2 @@ -47,7 +51,7 @@ */ @Documented @Retention(RUNTIME) -@Target({METHOD, ANNOTATION_TYPE}) +@Target({ METHOD, ANNOTATION_TYPE }) @EventListener(AfterTestExecutionEvent.class) public @interface AfterTestExecution { } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestMethod.java b/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestMethod.java index 89a6fc422cbb..d0da417bb5f4 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestMethod.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/annotation/AfterTestMethod.java @@ -16,30 +16,34 @@ package org.springframework.test.context.event.annotation; -import org.springframework.context.event.EventListener; -import org.springframework.test.context.event.AfterTestMethodEvent; - import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; +import org.springframework.context.event.EventListener; +import org.springframework.test.context.event.AfterTestMethodEvent; + import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * {@link EventListener} annotation used to consume {@link AfterTestMethodEvent}s published - * by {@link org.springframework.test.context.event.EventPublishingTestExecutionListener}. + * {@link EventListener @EventListener} annotation used to consume a + * {@link AfterTestMethodEvent} published by the + * {@link org.springframework.test.context.event.EventPublishingTestExecutionListener + * EventPublishingTestExecutionListener}. * - *

This annotation may be used on {@link EventListener}-compliant methods within the Spring test - * {@link org.springframework.context.ApplicationContext}, typically within - * {@link org.springframework.context.annotation.Configuration}s. A method annotated hereby will be - * called as part of the {@link org.springframework.test.context.TestExecutionListener#afterTestMethod(org.springframework.test.context.TestContext)} - * life-cycle method. + *

This annotation may be used on {@code @EventListener}-compliant methods within + * a Spring test {@link org.springframework.context.ApplicationContext ApplicationContext} + * — for example, on methods in a + * {@link org.springframework.context.annotation.Configuration @Configuration} + * class. A method annotated with this annotation will be invoked as part of the + * {@link org.springframework.test.context.TestExecutionListener#afterTestMethod} + * lifecycle. * - *

Make sure {@link org.springframework.test.context.event.EventPublishingTestExecutionListener} is enabled, - * for this annotation to have an effect, e.g. by annotation your test class with - * {@link org.springframework.test.context.TestExecutionListeners} accordingly. + *

The {@code EventPublishingTestExecutionListener} must be registered in order + * for this annotation to have an effect — for example, via + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}. * * @author Frank Scheffler * @since 5.2 @@ -47,7 +51,7 @@ */ @Documented @Retention(RUNTIME) -@Target({METHOD, ANNOTATION_TYPE}) +@Target({ METHOD, ANNOTATION_TYPE }) @EventListener(AfterTestMethodEvent.class) public @interface AfterTestMethod { } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestClass.java b/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestClass.java index 29badc02c3ea..df58f77291b1 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestClass.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestClass.java @@ -16,30 +16,34 @@ package org.springframework.test.context.event.annotation; -import org.springframework.context.event.EventListener; -import org.springframework.test.context.event.BeforeTestClassEvent; - import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; +import org.springframework.context.event.EventListener; +import org.springframework.test.context.event.BeforeTestClassEvent; + import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * {@link EventListener} annotation used to consume {@link BeforeTestClassEvent}s published - * by {@link org.springframework.test.context.event.EventPublishingTestExecutionListener}. + * {@link EventListener @EventListener} annotation used to consume a + * {@link BeforeTestClassEvent} published by the + * {@link org.springframework.test.context.event.EventPublishingTestExecutionListener + * EventPublishingTestExecutionListener}. * - *

This annotation may be used on {@link EventListener}-compliant methods within the Spring test - * {@link org.springframework.context.ApplicationContext}, typically within - * {@link org.springframework.context.annotation.Configuration}s. A method annotated hereby will be - * called as part of the {@link org.springframework.test.context.TestExecutionListener#beforeTestClass(org.springframework.test.context.TestContext)} - * life-cycle method. + *

This annotation may be used on {@code @EventListener}-compliant methods within + * a Spring test {@link org.springframework.context.ApplicationContext ApplicationContext} + * — for example, on methods in a + * {@link org.springframework.context.annotation.Configuration @Configuration} + * class. A method annotated with this annotation will be invoked as part of the + * {@link org.springframework.test.context.TestExecutionListener#beforeTestClass} + * lifecycle. * - *

Make sure {@link org.springframework.test.context.event.EventPublishingTestExecutionListener} is enabled, - * for this annotation to have an effect, e.g. by annotation your test class with - * {@link org.springframework.test.context.TestExecutionListeners} accordingly. + *

The {@code EventPublishingTestExecutionListener} must be registered in order + * for this annotation to have an effect — for example, via + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}. * * @author Frank Scheffler * @since 5.2 @@ -47,7 +51,7 @@ */ @Documented @Retention(RUNTIME) -@Target({METHOD, ANNOTATION_TYPE}) +@Target({ METHOD, ANNOTATION_TYPE }) @EventListener(BeforeTestClassEvent.class) public @interface BeforeTestClass { } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestExecution.java b/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestExecution.java index b9fa796b57c3..e8021737fa10 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestExecution.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestExecution.java @@ -16,30 +16,34 @@ package org.springframework.test.context.event.annotation; -import org.springframework.context.event.EventListener; -import org.springframework.test.context.event.BeforeTestExecutionEvent; - import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; +import org.springframework.context.event.EventListener; +import org.springframework.test.context.event.BeforeTestExecutionEvent; + import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * {@link EventListener} annotation used to consume {@link BeforeTestExecutionEvent}s published - * by {@link org.springframework.test.context.event.EventPublishingTestExecutionListener}. + * {@link EventListener @EventListener} annotation used to consume a + * {@link BeforeTestExecution} published by the + * {@link org.springframework.test.context.event.EventPublishingTestExecutionListener + * EventPublishingTestExecutionListener}. * - *

This annotation may be used on {@link EventListener}-compliant methods within the Spring test - * {@link org.springframework.context.ApplicationContext}, typically within - * {@link org.springframework.context.annotation.Configuration}s. A method annotated hereby will be - * called as part of the {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution(org.springframework.test.context.TestContext)} - * life-cycle method. + *

This annotation may be used on {@code @EventListener}-compliant methods within + * a Spring test {@link org.springframework.context.ApplicationContext ApplicationContext} + * — for example, on methods in a + * {@link org.springframework.context.annotation.Configuration @Configuration} + * class. A method annotated with this annotation will be invoked as part of the + * {@link org.springframework.test.context.TestExecutionListener#beforeTestExecution} + * lifecycle. * - *

Make sure {@link org.springframework.test.context.event.EventPublishingTestExecutionListener} is enabled, - * for this annotation to have an effect, e.g. by annotation your test class with - * {@link org.springframework.test.context.TestExecutionListeners} accordingly. + *

The {@code EventPublishingTestExecutionListener} must be registered in order + * for this annotation to have an effect — for example, via + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}. * * @author Frank Scheffler * @since 5.2 @@ -47,7 +51,7 @@ */ @Documented @Retention(RUNTIME) -@Target({METHOD, ANNOTATION_TYPE}) +@Target({ METHOD, ANNOTATION_TYPE }) @EventListener(BeforeTestExecutionEvent.class) public @interface BeforeTestExecution { } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestMethod.java b/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestMethod.java index d7a7483ffc28..eef8561286b3 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestMethod.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/annotation/BeforeTestMethod.java @@ -16,30 +16,34 @@ package org.springframework.test.context.event.annotation; -import org.springframework.context.event.EventListener; -import org.springframework.test.context.event.BeforeTestMethodEvent; - import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; +import org.springframework.context.event.EventListener; +import org.springframework.test.context.event.BeforeTestMethodEvent; + import static java.lang.annotation.ElementType.ANNOTATION_TYPE; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * {@link EventListener} annotation used to consume {@link BeforeTestMethodEvent}s published - * by {@link org.springframework.test.context.event.EventPublishingTestExecutionListener}. + * {@link EventListener @EventListener} annotation used to consume a + * {@link BeforeTestMethodEvent} published by the + * {@link org.springframework.test.context.event.EventPublishingTestExecutionListener + * EventPublishingTestExecutionListener}. * - *

This annotation may be used on {@link EventListener}-compliant methods within the Spring test - * {@link org.springframework.context.ApplicationContext}, typically within - * {@link org.springframework.context.annotation.Configuration}s. A method annotated hereby will be - * called as part of the {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod(org.springframework.test.context.TestContext)} - * life-cycle method. + *

This annotation may be used on {@code @EventListener}-compliant methods within + * a Spring test {@link org.springframework.context.ApplicationContext ApplicationContext} + * — for example, on methods in a + * {@link org.springframework.context.annotation.Configuration @Configuration} + * class. A method annotated with this annotation will be invoked as part of the + * {@link org.springframework.test.context.TestExecutionListener#beforeTestMethod} + * lifecycle. * - *

Make sure {@link org.springframework.test.context.event.EventPublishingTestExecutionListener} is enabled, - * for this annotation to have an effect, e.g. by annotation your test class with - * {@link org.springframework.test.context.TestExecutionListeners} accordingly. + *

The {@code EventPublishingTestExecutionListener} must be registered in order + * for this annotation to have an effect — for example, via + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}. * * @author Frank Scheffler * @since 5.2 @@ -47,7 +51,7 @@ */ @Documented @Retention(RUNTIME) -@Target({METHOD, ANNOTATION_TYPE}) +@Target({ METHOD, ANNOTATION_TYPE }) @EventListener(BeforeTestMethodEvent.class) public @interface BeforeTestMethod { } diff --git a/spring-test/src/main/java/org/springframework/test/context/event/annotation/PrepareTestInstance.java b/spring-test/src/main/java/org/springframework/test/context/event/annotation/PrepareTestInstance.java index e410c58552ef..3509aeb5e84a 100644 --- a/spring-test/src/main/java/org/springframework/test/context/event/annotation/PrepareTestInstance.java +++ b/spring-test/src/main/java/org/springframework/test/context/event/annotation/PrepareTestInstance.java @@ -28,18 +28,22 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * {@link EventListener} annotation used to consume {@link PrepareTestInstanceEvent}s published - * by {@link org.springframework.test.context.event.EventPublishingTestExecutionListener}. + * {@link EventListener @EventListener} annotation used to consume a + * {@link PrepareTestInstanceEvent} published by the + * {@link org.springframework.test.context.event.EventPublishingTestExecutionListener + * EventPublishingTestExecutionListener}. * - *

This annotation may be used on {@link EventListener}-compliant methods within the Spring test - * {@link org.springframework.context.ApplicationContext}, typically within - * {@link org.springframework.context.annotation.Configuration}s. A method annotated hereby will be - * called as part of the {@link org.springframework.test.context.TestExecutionListener#prepareTestInstance(org.springframework.test.context.TestContext)} - * life-cycle method. + *

This annotation may be used on {@code @EventListener}-compliant methods within + * a Spring test {@link org.springframework.context.ApplicationContext ApplicationContext} + * — for example, on methods in a + * {@link org.springframework.context.annotation.Configuration @Configuration} + * class. A method annotated with this annotation will be invoked as part of the + * {@link org.springframework.test.context.TestExecutionListener#prepareTestInstance} + * lifecycle. * - *

Make sure {@link org.springframework.test.context.event.EventPublishingTestExecutionListener} is enabled, - * for this annotation to have an effect, e.g. by annotation your test class with - * {@link org.springframework.test.context.TestExecutionListeners} accordingly. + *

The {@code EventPublishingTestExecutionListener} must be registered in order + * for this annotation to have an effect — for example, via + * {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}. * * @author Frank Scheffler * @since 5.2 @@ -47,7 +51,7 @@ */ @Documented @Retention(RUNTIME) -@Target({METHOD, ANNOTATION_TYPE}) +@Target({ METHOD, ANNOTATION_TYPE }) @EventListener(PrepareTestInstanceEvent.class) public @interface PrepareTestInstance { } diff --git a/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java index 910e107c25a4..46ed5efa0c3a 100644 --- a/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerIntegrationTests.java @@ -20,6 +20,7 @@ import org.junit.Before; import org.junit.Test; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.ContextConfiguration; @@ -27,13 +28,22 @@ import org.springframework.test.context.TestContextManager; import org.springframework.test.context.TestExecutionListener; import org.springframework.test.context.TestExecutionListeners; -import org.springframework.test.context.event.annotation.*; +import org.springframework.test.context.event.annotation.AfterTestClass; +import org.springframework.test.context.event.annotation.AfterTestExecution; +import org.springframework.test.context.event.annotation.AfterTestMethod; +import org.springframework.test.context.event.annotation.BeforeTestClass; +import org.springframework.test.context.event.annotation.BeforeTestExecution; +import org.springframework.test.context.event.annotation.BeforeTestMethod; +import org.springframework.test.context.event.annotation.PrepareTestInstance; import org.springframework.util.ReflectionUtils; -import static org.mockito.BDDMockito.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.only; +import static org.mockito.Mockito.reset; +import static org.mockito.Mockito.verify; /** - * Integration test for {@link EventPublishingTestExecutionListener} and + * Integration tests for {@link EventPublishingTestExecutionListener} and * accompanying {@link TestContextEvent} annotations. * * @author Frank Scheffler @@ -41,130 +51,134 @@ */ public class EventPublishingTestExecutionListenerIntegrationTests { - private TestContextManager testContextManager; - private TestContext testContext; - private TestExecutionListener trigger; - private Object testInstance; - private Method testMethod; - - @Configuration - static class EventCaptureConfiguration { - @Bean - public TestExecutionListener trigger() { - return mock(TestExecutionListener.class); - } - - @BeforeTestClass - public void beforeTestClass(BeforeTestClassEvent e) throws Exception { - trigger().beforeTestClass(e.getSource()); - } - - @PrepareTestInstance - public void prepareTestInstance(PrepareTestInstanceEvent e) throws Exception { - trigger().prepareTestInstance(e.getSource()); - } - - - @BeforeTestMethod - public void beforeTestMethod(BeforeTestMethodEvent e) throws Exception { - trigger().beforeTestMethod(e.getSource()); - } - - @BeforeTestExecution - public void beforeTestExecutiob(BeforeTestExecutionEvent e) throws Exception { - trigger().beforeTestExecution(e.getSource()); - } - - @AfterTestExecution - public void afterTestExecution(AfterTestExecutionEvent e) throws Exception { - trigger().afterTestExecution(e.getSource()); - } - - @AfterTestMethod - public void afterTestMethod(AfterTestMethodEvent e) throws Exception { - trigger().afterTestMethod(e.getSource()); - } - - @AfterTestClass - public void afterTestClass(AfterTestClassEvent e) throws Exception { - trigger().afterTestClass(e.getSource()); - } - - } - - @ContextConfiguration(classes = EventCaptureConfiguration.class) - @TestExecutionListeners(EventPublishingTestExecutionListener.class) - static class EmptyTestCase { - - /** - * Serves as dummy test method. - */ - @SuppressWarnings("PMD.UncommentedEmptyMethodBody") - public void dummyTestMethod() { - } - } - - static class TestContextExposingTestContextManager extends TestContextManager { - public TestContextExposingTestContextManager() { - super(EmptyTestCase.class); - } - - public TestContext getProtectedTestContext() { - return getTestContext(); - } - } - - @Before - public void initialize() { - TestContextExposingTestContextManager tcm = new TestContextExposingTestContextManager(); - testContextManager = tcm; - testContext = tcm.getProtectedTestContext(); - trigger = testContext.getApplicationContext().getBean(EventCaptureConfiguration.class).trigger(); - // reset because mock is a cached context bean - reset(trigger); - testInstance = new EmptyTestCase(); - testMethod = ReflectionUtils.findMethod(EmptyTestCase.class, "dummyMethod"); - } - - @Test - public void beforeTestClassAnnotation() throws Exception { - testContextManager.beforeTestClass(); - verify(trigger, only()).beforeTestClass(testContext); - } - - @Test - public void prepareTestInstanceAnnotation() throws Exception { - testContextManager.prepareTestInstance(testInstance); - verify(trigger, only()).prepareTestInstance(testContext); - } - - @Test - public void beforeTestMethodAnnotation() throws Exception { - testContextManager.beforeTestMethod(testInstance, testMethod); - verify(trigger, only()).beforeTestMethod(testContext); - } - - @Test - public void beforeTestExecutionAnnotation() throws Exception { - testContextManager.beforeTestExecution(testInstance, testMethod); - verify(trigger, only()).beforeTestExecution(testContext); - } - - @Test - public void afterTestExecutionAnnotation() throws Exception { - testContextManager.afterTestExecution(testInstance, testMethod, null); - verify(trigger, only()).afterTestExecution(testContext); - } - - @Test - public void afterTestMethodAnnotation() throws Exception { - testContextManager.afterTestMethod(testInstance, testMethod, null); - verify(trigger, only()).afterTestMethod(testContext); - } - - @Test - public void afterTestClassAnnotation() throws Exception { - testContextManager.afterTestClass(); - verify(trigger, only()).afterTestClass(testContext); - } + private TestContextManager testContextManager; + private TestContext testContext; + private TestExecutionListener listener; + private Object testInstance; + private Method testMethod; + + + @Before + public void initialize() { + TestContextExposingTestContextManager tcm = new TestContextExposingTestContextManager(); + testContextManager = tcm; + testContext = tcm.getProtectedTestContext(); + listener = testContext.getApplicationContext().getBean(EventCaptureConfiguration.class).trigger(); + // reset because mock is a cached context bean + reset(listener); + testInstance = new EmptyTestCase(); + testMethod = ReflectionUtils.findMethod(EmptyTestCase.class, "dummyMethod"); + } + + @Test + public void beforeTestClassAnnotation() throws Exception { + testContextManager.beforeTestClass(); + verify(listener, only()).beforeTestClass(testContext); + } + + @Test + public void prepareTestInstanceAnnotation() throws Exception { + testContextManager.prepareTestInstance(testInstance); + verify(listener, only()).prepareTestInstance(testContext); + } + + @Test + public void beforeTestMethodAnnotation() throws Exception { + testContextManager.beforeTestMethod(testInstance, testMethod); + verify(listener, only()).beforeTestMethod(testContext); + } + + @Test + public void beforeTestExecutionAnnotation() throws Exception { + testContextManager.beforeTestExecution(testInstance, testMethod); + verify(listener, only()).beforeTestExecution(testContext); + } + + @Test + public void afterTestExecutionAnnotation() throws Exception { + testContextManager.afterTestExecution(testInstance, testMethod, null); + verify(listener, only()).afterTestExecution(testContext); + } + + @Test + public void afterTestMethodAnnotation() throws Exception { + testContextManager.afterTestMethod(testInstance, testMethod, null); + verify(listener, only()).afterTestMethod(testContext); + } + + @Test + public void afterTestClassAnnotation() throws Exception { + testContextManager.afterTestClass(); + verify(listener, only()).afterTestClass(testContext); + } + + + @Configuration + static class EventCaptureConfiguration { + + @Bean + public TestExecutionListener trigger() { + return mock(TestExecutionListener.class); + } + + @BeforeTestClass + public void beforeTestClass(BeforeTestClassEvent e) throws Exception { + trigger().beforeTestClass(e.getSource()); + } + + @PrepareTestInstance + public void prepareTestInstance(PrepareTestInstanceEvent e) throws Exception { + trigger().prepareTestInstance(e.getSource()); + } + + @BeforeTestMethod + public void beforeTestMethod(BeforeTestMethodEvent e) throws Exception { + trigger().beforeTestMethod(e.getSource()); + } + + @BeforeTestExecution + public void beforeTestExecutiob(BeforeTestExecutionEvent e) throws Exception { + trigger().beforeTestExecution(e.getSource()); + } + + @AfterTestExecution + public void afterTestExecution(AfterTestExecutionEvent e) throws Exception { + trigger().afterTestExecution(e.getSource()); + } + + @AfterTestMethod + public void afterTestMethod(AfterTestMethodEvent e) throws Exception { + trigger().afterTestMethod(e.getSource()); + } + + @AfterTestClass + public void afterTestClass(AfterTestClassEvent e) throws Exception { + trigger().afterTestClass(e.getSource()); + } + + } + + @ContextConfiguration(classes = EventCaptureConfiguration.class) + @TestExecutionListeners(EventPublishingTestExecutionListener.class) + static class EmptyTestCase { + + /** + * Serves as dummy test method. + */ + @SuppressWarnings("PMD.UncommentedEmptyMethodBody") + public void dummyTestMethod() { + } + } + + static class TestContextExposingTestContextManager extends TestContextManager { + + public TestContextExposingTestContextManager() { + super(EmptyTestCase.class); + } + + public TestContext getProtectedTestContext() { + return getTestContext(); + } + } + } diff --git a/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerTests.java b/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerTests.java index f803dec7c919..7eeb2ab9f727 100644 --- a/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/event/EventPublishingTestExecutionListenerTests.java @@ -31,9 +31,8 @@ import static org.mockito.BDDMockito.only; import static org.mockito.BDDMockito.verify; - /** - * Unit test for {@link EventPublishingTestExecutionListener}. + * Unit tests for {@link EventPublishingTestExecutionListener}. * * @author Frank Scheffler * @since 5.2 @@ -41,60 +40,61 @@ @RunWith(MockitoJUnitRunner.class) public class EventPublishingTestExecutionListenerTests { - @Mock(answer=Answers.RETURNS_DEEP_STUBS) + @Mock(answer = Answers.RETURNS_DEEP_STUBS) private TestContext testContext; - + @Captor private ArgumentCaptor testExecutionEvent; - - - private final EventPublishingTestExecutionListener cut = new EventPublishingTestExecutionListener(); - - private void assertEvent(Class eventClass) { - verify(testContext.getApplicationContext(), only()).publishEvent(testExecutionEvent.capture()); - assertThat(testExecutionEvent.getValue(), instanceOf(eventClass)); - assertThat(testExecutionEvent.getValue().getSource(), equalTo(testContext)); - } - + + private final EventPublishingTestExecutionListener listener = new EventPublishingTestExecutionListener(); + + @Test public void publishBeforeClassTestExecutionEvent() { - cut.beforeTestClass(testContext); + listener.beforeTestClass(testContext); assertEvent(BeforeTestClassEvent.class); } - + @Test public void publishPrepareInstanceTestExecutionEvent() { - cut.prepareTestInstance(testContext); + listener.prepareTestInstance(testContext); assertEvent(PrepareTestInstanceEvent.class); } @Test public void publishBeforeMethodTestExecutionEvent() { - cut.beforeTestMethod(testContext); + listener.beforeTestMethod(testContext); assertEvent(BeforeTestMethodEvent.class); } @Test public void publishBeforeExecutionTestExecutionEvent() { - cut.beforeTestExecution(testContext); + listener.beforeTestExecution(testContext); assertEvent(BeforeTestExecutionEvent.class); } @Test public void publishAfterExecutionTestExecutionEvent() { - cut.afterTestExecution(testContext); + listener.afterTestExecution(testContext); assertEvent(AfterTestExecutionEvent.class); } @Test public void publishAfterMethodTestExecutionEvent() { - cut.afterTestMethod(testContext); + listener.afterTestMethod(testContext); assertEvent(AfterTestMethodEvent.class); } @Test public void publishAfterClassTestExecutionEvent() { - cut.afterTestClass(testContext); + listener.afterTestClass(testContext); assertEvent(AfterTestClassEvent.class); } + + private void assertEvent(Class eventClass) { + verify(testContext.getApplicationContext(), only()).publishEvent(testExecutionEvent.capture()); + assertThat(testExecutionEvent.getValue(), instanceOf(eventClass)); + assertThat(testExecutionEvent.getValue().getSource(), equalTo(testContext)); + } + }