Introduce ApplicationEvents to assert events published during tests
This commit introduces a new feature in the Spring TestContext
Framework (TCF) that provides support for recording application events
published in the ApplicationContext so that assertions can be performed
against those events within tests. All events published during the
execution of a single test are made available via the ApplicationEvents
API which allows one to process the events as a java.util.Stream.
The following example demonstrates usage of this new feature.
@SpringJUnitConfig(/* ... */)
@RecordApplicationEvents
class OrderServiceTests {
@Autowired
OrderService orderService;
@Autowired
ApplicationEvents events;
@Test
void submitOrder() {
// Invoke method in OrderService that publishes an event
orderService.submitOrder(new Order(/* ... */));
// Verify that an OrderSubmitted event was published
int numEvents = events.stream(OrderSubmitted.class).count();
assertThat(numEvents).isEqualTo(1);
}
}
To enable the feature, a test class must be annotated or meta-annotated
with @RecordApplicationEvents. Behind the scenes, a new
ApplicationEventsTestExecutionListener manages the registration of
ApplicationEvents for the current thread at various points within the
test execution lifecycle and makes the current instance of
ApplicationEvents available to tests via an @Autowired field in the
test class. The latter is made possible by a custom ObjectFactory that
is registered as a "resolvable dependency". Thanks to the magic of
ObjectFactoryDelegatingInvocationHandler in the spring-beans module,
the ApplicationEvents instance injected into test classes is
effectively a scoped-proxy that always accesses the ApplicationEvents
managed for the current test.
The current ApplicationEvents instance is stored in a ThreadLocal
variable which is made available in the ApplicationEventsHolder.
Although this class is public, it is only intended for use within the
TCF or in the implementation of third-party extensions.
ApplicationEventsApplicationListener is responsible for listening to
all application events and recording them in the current
ApplicationEvents instance. A single
ApplicationEventsApplicationListener is registered with the test's
ApplicationContext by the ApplicationEventsTestExecutionListener.
The SpringExtension has also been updated to support parameters of type
ApplicationEvents via the JUnit Jupiter ParameterResolver extension
API. This allows JUnit Jupiter based tests to receive access to the
current ApplicationEvents via test and lifecycle method parameters as
an alternative to @Autowired fields in the test class.
Closes gh-25616
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -48,6 +48,8 @@ package org.springframework.test.context;
|
||||
* ServletTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
|
||||
* DirtiesContextBeforeModesTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener
|
||||
* ApplicationEventsTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener
|
||||
* DependencyInjectionTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener
|
||||
|
||||
@@ -67,6 +67,7 @@ public @interface TestExecutionListeners {
|
||||
* {@link #value}, but it may be used instead of {@link #value}.
|
||||
* @see org.springframework.test.context.web.ServletTestExecutionListener
|
||||
* @see org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
|
||||
* @see org.springframework.test.context.event.ApplicationEventsTestExecutionListener
|
||||
* @see org.springframework.test.context.support.DependencyInjectionTestExecutionListener
|
||||
* @see org.springframework.test.context.support.DirtiesContextTestExecutionListener
|
||||
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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
|
||||
*
|
||||
* https://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 java.util.stream.Stream;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
|
||||
/**
|
||||
* {@code ApplicationEvents} encapsulates all {@linkplain ApplicationEvent
|
||||
* application events} that were fired during the execution of a single test method.
|
||||
*
|
||||
* <p>To use {@code ApplicationEvents} in your tests, do the following.
|
||||
* <ul>
|
||||
* <li>Ensure that your test class is annotated or meta-annotated with
|
||||
* {@link RecordApplicationEvents @RecordApplicationEvents}.</li>
|
||||
* <li>Ensure that the {@link ApplicationEventsTestExecutionListener} is
|
||||
* registered. Note, however, that it is registered by default and only needs
|
||||
* to be manually registered if you have custom configuration via
|
||||
* {@link org.springframework.test.context.TestExecutionListeners @TestExecutionListeners}
|
||||
* that does not include the default listeners.</li>
|
||||
* <li>Annotate a field of type {@code ApplicationEvents} with
|
||||
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired} and
|
||||
* use that instance of {@code ApplicationEvents} in your test and lifecycle methods.</li>
|
||||
* <li>With JUnit Jupiter, you may optionally declare a parameter of type
|
||||
* {@code ApplicationEvents} in a test or lifecycle method as an alternative to
|
||||
* an {@code @Autowired} field in the test class.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Oliver Drotbohm
|
||||
* @since 5.3.3
|
||||
* @see RecordApplicationEvents
|
||||
* @see ApplicationEventsTestExecutionListener
|
||||
* @see org.springframework.context.ApplicationEvent
|
||||
*/
|
||||
public interface ApplicationEvents {
|
||||
|
||||
/**
|
||||
* Stream all application events that were fired during test execution.
|
||||
* @return a stream of all application events
|
||||
* @see #stream(Class)
|
||||
* @see #clear()
|
||||
*/
|
||||
Stream<ApplicationEvent> stream();
|
||||
|
||||
/**
|
||||
* Stream all application events or event payloads of the given type that
|
||||
* were fired during test execution.
|
||||
* @param <T> the event type
|
||||
* @param type the type of events or payloads to stream; never {@code null}
|
||||
* @return a stream of all application events or event payloads of the
|
||||
* specified type
|
||||
* @see #stream()
|
||||
* @see #clear()
|
||||
*/
|
||||
<T> Stream<T> stream(Class<T> type);
|
||||
|
||||
/**
|
||||
* Clear all application events recorded by this {@code ApplicationEvents} instance.
|
||||
* <p>Subsequent calls to {@link #stream()} or {@link #stream(Class)} will
|
||||
* only include events recorded since this method was invoked.
|
||||
* @see #stream()
|
||||
* @see #stream(Class)
|
||||
*/
|
||||
void clear();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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
|
||||
*
|
||||
* https://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.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
|
||||
/**
|
||||
* {@link ApplicationListener} that listens to all events and adds them to the
|
||||
* current {@link ApplicationEvents} instance if registered for the current thread.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Oliver Drotbohm
|
||||
* @since 5.3.3
|
||||
*/
|
||||
class ApplicationEventsApplicationListener implements ApplicationListener<ApplicationEvent> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
DefaultApplicationEvents applicationEvents =
|
||||
(DefaultApplicationEvents) ApplicationEventsHolder.getApplicationEvents();
|
||||
if (applicationEvents != null) {
|
||||
applicationEvents.addEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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
|
||||
*
|
||||
* https://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.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Holder class to expose the application events published during the execution
|
||||
* of a test in the form of a thread-bound {@link ApplicationEvents} object.
|
||||
*
|
||||
* <p>{@code ApplicationEvents} are registered in this holder and managed by
|
||||
* the {@link ApplicationEventsTestExecutionListener}.
|
||||
*
|
||||
* <p>Although this class is {@code public}, it is only intended for use within
|
||||
* the <em>Spring TestContext Framework</em> or in the implementation of
|
||||
* third-party extensions. Test authors should therefore allow the current
|
||||
* instance of {@code ApplicationEvents} to be
|
||||
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
|
||||
* into a field in the test class or injected via a parameter in test and
|
||||
* lifecycle methods when using JUnit Jupiter and the {@link
|
||||
* org.springframework.test.context.junit.jupiter.SpringExtension SpringExtension}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Oliver Drotbohm
|
||||
* @since 5.3.3
|
||||
* @see ApplicationEvents
|
||||
* @see RecordApplicationEvents
|
||||
* @see ApplicationEventsTestExecutionListener
|
||||
*/
|
||||
public abstract class ApplicationEventsHolder {
|
||||
|
||||
private static final ThreadLocal<DefaultApplicationEvents> applicationEvents = new ThreadLocal<>();
|
||||
|
||||
|
||||
private ApplicationEventsHolder() {
|
||||
// no-op to prevent instantiation of this holder class
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the {@link ApplicationEvents} for the current thread.
|
||||
* @return the current {@code ApplicationEvents}, or {@code null} if not registered
|
||||
*/
|
||||
@Nullable
|
||||
public static ApplicationEvents getApplicationEvents() {
|
||||
return applicationEvents.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link ApplicationEvents} for the current thread.
|
||||
* @return the current {@code ApplicationEvents}
|
||||
* @throws IllegalStateException if an instance of {@code ApplicationEvents}
|
||||
* has not been registered for the current thread
|
||||
*/
|
||||
@Nullable
|
||||
public static ApplicationEvents getRequiredApplicationEvents() {
|
||||
ApplicationEvents events = applicationEvents.get();
|
||||
Assert.state(events != null, "Failed to retrieve ApplicationEvents for the current thread. " +
|
||||
"Ensure that your test class is annotated with @RecordApplicationEvents " +
|
||||
"and that the ApplicationEventsTestExecutionListener is registered.");
|
||||
return events;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register a new {@link DefaultApplicationEvents} instance to be used for the
|
||||
* current thread, if necessary.
|
||||
* <p>If {@link #registerApplicationEvents()} has already been called for the
|
||||
* current thread, this method does not do anything.
|
||||
*/
|
||||
static void registerApplicationEventsIfNecessary() {
|
||||
if (getApplicationEvents() == null) {
|
||||
registerApplicationEvents();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new {@link DefaultApplicationEvents} instance to be used for the
|
||||
* current thread.
|
||||
*/
|
||||
static void registerApplicationEvents() {
|
||||
applicationEvents.set(new DefaultApplicationEvents());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the registration of the {@link ApplicationEvents} for the current thread.
|
||||
*/
|
||||
static void unregisterApplicationEvents() {
|
||||
applicationEvents.remove();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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
|
||||
*
|
||||
* https://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 java.io.Serializable;
|
||||
|
||||
import org.springframework.beans.factory.ObjectFactory;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.AbstractApplicationContext;
|
||||
import org.springframework.core.Conventions;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@code TestExecutionListener} which provides support for {@link ApplicationEvents}.
|
||||
*
|
||||
* <p>This listener manages the registration of {@code ApplicationEvents} for the
|
||||
* current thread at various points within the test execution lifecycle and makes
|
||||
* the current instance of {@code ApplicationEvents} available to tests via an
|
||||
* {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
|
||||
* field in the test class.
|
||||
*
|
||||
* <p>If the test class is not annotated or meta-annotated with
|
||||
* {@link RecordApplicationEvents @RecordApplicationEvents}, this listener
|
||||
* effectively does nothing.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 5.3.3
|
||||
* @see ApplicationEvents
|
||||
* @see ApplicationEventsHolder
|
||||
*/
|
||||
public class ApplicationEventsTestExecutionListener extends AbstractTestExecutionListener {
|
||||
|
||||
/**
|
||||
* Attribute name for a {@link TestContext} attribute which indicates
|
||||
* whether the test class for the given test context is annotated with
|
||||
* {@link RecordApplicationEvents @RecordApplicationEvents}.
|
||||
* <p>Permissible values include {@link Boolean#TRUE} and {@link Boolean#FALSE}.
|
||||
*/
|
||||
private static final String RECORD_APPLICATION_EVENTS = Conventions.getQualifiedAttributeName(
|
||||
ApplicationEventsTestExecutionListener.class, "recordApplicationEvents");
|
||||
|
||||
private static final Object applicationEventsMonitor = new Object();
|
||||
|
||||
|
||||
/**
|
||||
* Returns {@code 1800}.
|
||||
*/
|
||||
@Override
|
||||
public final int getOrder() {
|
||||
return 1800;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prepareTestInstance(TestContext testContext) throws Exception {
|
||||
if (recordApplicationEvents(testContext)) {
|
||||
registerListenerAndResolvableDependencyIfNecessary(testContext.getApplicationContext());
|
||||
ApplicationEventsHolder.registerApplicationEvents();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void beforeTestMethod(TestContext testContext) throws Exception {
|
||||
if (recordApplicationEvents(testContext)) {
|
||||
// Register a new ApplicationEvents instance for the current thread
|
||||
// in case the test instance is shared -- for example, in TestNG or
|
||||
// JUnit Jupiter with @TestInstance(PER_CLASS) semantics.
|
||||
ApplicationEventsHolder.registerApplicationEventsIfNecessary();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterTestMethod(TestContext testContext) throws Exception {
|
||||
if (recordApplicationEvents(testContext)) {
|
||||
ApplicationEventsHolder.unregisterApplicationEvents();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean recordApplicationEvents(TestContext testContext) {
|
||||
return testContext.computeAttribute(RECORD_APPLICATION_EVENTS, name ->
|
||||
TestContextAnnotationUtils.hasAnnotation(testContext.getTestClass(), RecordApplicationEvents.class));
|
||||
}
|
||||
|
||||
private void registerListenerAndResolvableDependencyIfNecessary(ApplicationContext applicationContext) {
|
||||
Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext,
|
||||
"The ApplicationContext for the test must be an AbstractApplicationContext");
|
||||
AbstractApplicationContext aac = (AbstractApplicationContext) applicationContext;
|
||||
// Synchronize to avoid race condition in parallel test execution
|
||||
synchronized(applicationEventsMonitor) {
|
||||
boolean notAlreadyRegistered = aac.getApplicationListeners().stream()
|
||||
.map(Object::getClass)
|
||||
.noneMatch(ApplicationEventsApplicationListener.class::equals);
|
||||
if (notAlreadyRegistered) {
|
||||
// Register a new ApplicationEventsApplicationListener.
|
||||
aac.addApplicationListener(new ApplicationEventsApplicationListener());
|
||||
|
||||
// Register ApplicationEvents as a resolvable dependency for @Autowired support in test classes.
|
||||
ConfigurableListableBeanFactory beanFactory = aac.getBeanFactory();
|
||||
beanFactory.registerResolvableDependency(ApplicationEvents.class, new ApplicationEventsObjectFactory());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory that exposes the current {@link ApplicationEvents} object on demand.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
private static class ApplicationEventsObjectFactory implements ObjectFactory<ApplicationEvents>, Serializable {
|
||||
|
||||
@Override
|
||||
public ApplicationEvents getObject() {
|
||||
return ApplicationEventsHolder.getRequiredApplicationEvents();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Current ApplicationEvents";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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
|
||||
*
|
||||
* https://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 java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.PayloadApplicationEvent;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link ApplicationEvents}.
|
||||
*
|
||||
* @author Oliver Drotbohm
|
||||
* @author Sam Brannen
|
||||
* @since 5.3.3
|
||||
*/
|
||||
class DefaultApplicationEvents implements ApplicationEvents {
|
||||
|
||||
private final List<ApplicationEvent> events = new ArrayList<>();
|
||||
|
||||
|
||||
void addEvent(ApplicationEvent event) {
|
||||
this.events.add(event);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stream<ApplicationEvent> stream() {
|
||||
return this.events.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Stream<T> stream(Class<T> type) {
|
||||
return this.events.stream()
|
||||
.map(this::unwrapPayloadEvent)
|
||||
.filter(type::isInstance)
|
||||
.map(type::cast);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
this.events.clear();
|
||||
}
|
||||
|
||||
private Object unwrapPayloadEvent(Object source) {
|
||||
return (PayloadApplicationEvent.class.isInstance(source) ?
|
||||
((PayloadApplicationEvent<?>) source).getPayload() : source);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2002-2020 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
|
||||
*
|
||||
* https://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 java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Inherited;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* {@code @RecordApplicationEvents} is a class-level annotation that is used to
|
||||
* instruct the <em>Spring TestContext Framework</em> to record all
|
||||
* {@linkplain org.springframework.context.ApplicationEvent application events}
|
||||
* that are published in the {@link org.springframework.context.ApplicationContext
|
||||
* ApplicationContext} during the execution of a single test.
|
||||
*
|
||||
* <p>The recorded events can be accessed via the {@link ApplicationEvents} API
|
||||
* within your tests.
|
||||
*
|
||||
* <p>This annotation may be used as a <em>meta-annotation</em> to create custom
|
||||
* <em>composed annotations</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 5.3.3
|
||||
* @see ApplicationEvents
|
||||
* @see ApplicationEventsTestExecutionListener
|
||||
*/
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface RecordApplicationEvents {
|
||||
}
|
||||
@@ -52,6 +52,7 @@ import org.springframework.core.annotation.RepeatableContainers;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.TestConstructor;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.event.ApplicationEvents;
|
||||
import org.springframework.test.context.support.PropertyProvider;
|
||||
import org.springframework.test.context.support.TestConstructorUtils;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -218,6 +219,7 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
||||
* invoked with a fallback {@link PropertyProvider} that delegates its lookup
|
||||
* to {@link ExtensionContext#getConfigurationParameter(String)}.</li>
|
||||
* <li>The parameter is of type {@link ApplicationContext} or a sub-type thereof.</li>
|
||||
* <li>The parameter is of type {@link ApplicationEvents} or a sub-type thereof.</li>
|
||||
* <li>{@link ParameterResolutionDelegate#isAutowirable} returns {@code true}.</li>
|
||||
* </ol>
|
||||
* <p><strong>WARNING</strong>: If a test class {@code Constructor} is annotated
|
||||
@@ -238,9 +240,19 @@ public class SpringExtension implements BeforeAllCallback, AfterAllCallback, Tes
|
||||
extensionContext.getConfigurationParameter(propertyName).orElse(null);
|
||||
return (TestConstructorUtils.isAutowirableConstructor(executable, testClass, junitPropertyProvider) ||
|
||||
ApplicationContext.class.isAssignableFrom(parameter.getType()) ||
|
||||
supportsApplicationEvents(parameterContext) ||
|
||||
ParameterResolutionDelegate.isAutowirable(parameter, parameterContext.getIndex()));
|
||||
}
|
||||
|
||||
private boolean supportsApplicationEvents(ParameterContext parameterContext) {
|
||||
if (ApplicationEvents.class.isAssignableFrom(parameterContext.getParameter().getType())) {
|
||||
Assert.isTrue(parameterContext.getDeclaringExecutable() instanceof Method,
|
||||
"ApplicationEvents can only be injected into test and lifecycle methods");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a value for the {@link Parameter} in the supplied {@link ParameterContext} by
|
||||
* retrieving the corresponding dependency from the test's {@link ApplicationContext}.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -27,6 +27,7 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
|
||||
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
|
||||
@@ -54,6 +55,7 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
* <ul>
|
||||
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener}
|
||||
@@ -82,6 +84,7 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
* @see TestExecutionListeners
|
||||
* @see ServletTestExecutionListener
|
||||
* @see DirtiesContextBeforeModesTestExecutionListener
|
||||
* @see ApplicationEventsTestExecutionListener
|
||||
* @see DependencyInjectionTestExecutionListener
|
||||
* @see DirtiesContextTestExecutionListener
|
||||
* @see EventPublishingTestExecutionListener
|
||||
@@ -90,8 +93,8 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
|
||||
DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
|
||||
EventPublishingTestExecutionListener.class })
|
||||
ApplicationEventsTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
|
||||
DirtiesContextTestExecutionListener.class, EventPublishingTestExecutionListener.class })
|
||||
public abstract class AbstractJUnit4SpringContextTests implements ApplicationContextAware {
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -27,6 +27,7 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
|
||||
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
|
||||
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
@@ -62,6 +63,7 @@ import org.springframework.util.Assert;
|
||||
* <ul>
|
||||
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.transaction.TransactionalTestExecutionListener}
|
||||
@@ -100,8 +102,8 @@ import org.springframework.util.Assert;
|
||||
* @see org.springframework.test.jdbc.JdbcTestUtils
|
||||
* @see org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests
|
||||
*/
|
||||
@TestExecutionListeners(listeners = { ServletTestExecutionListener.class,
|
||||
DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
|
||||
@TestExecutionListeners(listeners = { ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
|
||||
ApplicationEventsTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
|
||||
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
|
||||
SqlScriptsTestExecutionListener.class, EventPublishingTestExecutionListener.class }, inheritListeners = false)
|
||||
@Transactional
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -36,6 +36,7 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestContextManager;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
|
||||
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener;
|
||||
@@ -64,6 +65,7 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
* <ul>
|
||||
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.event.EventPublishingTestExecutionListener}
|
||||
@@ -78,6 +80,7 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
* @see TestExecutionListeners
|
||||
* @see ServletTestExecutionListener
|
||||
* @see DirtiesContextBeforeModesTestExecutionListener
|
||||
* @see ApplicationEventsTestExecutionListener
|
||||
* @see DependencyInjectionTestExecutionListener
|
||||
* @see DirtiesContextTestExecutionListener
|
||||
* @see EventPublishingTestExecutionListener
|
||||
@@ -85,8 +88,8 @@ import org.springframework.test.context.web.ServletTestExecutionListener;
|
||||
* @see org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests
|
||||
*/
|
||||
@TestExecutionListeners({ ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
|
||||
DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class,
|
||||
EventPublishingTestExecutionListener.class })
|
||||
ApplicationEventsTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
|
||||
DirtiesContextTestExecutionListener.class, EventPublishingTestExecutionListener.class })
|
||||
public abstract class AbstractTestNGSpringContextTests implements IHookable, ApplicationContextAware {
|
||||
|
||||
/** Logger available to subclasses. */
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -26,6 +26,7 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.test.context.TestExecutionListeners;
|
||||
import org.springframework.test.context.event.ApplicationEventsTestExecutionListener;
|
||||
import org.springframework.test.context.event.EventPublishingTestExecutionListener;
|
||||
import org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
@@ -61,6 +62,7 @@ import org.springframework.util.Assert;
|
||||
* <ul>
|
||||
* <li>{@link org.springframework.test.context.web.ServletTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.event.ApplicationEventsTestExecutionListener}</li>
|
||||
* <li>{@link org.springframework.test.context.support.DependencyInjectionTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.support.DirtiesContextTestExecutionListener}
|
||||
* <li>{@link org.springframework.test.context.transaction.TransactionalTestExecutionListener}
|
||||
@@ -84,8 +86,8 @@ import org.springframework.util.Assert;
|
||||
* @see org.springframework.test.jdbc.JdbcTestUtils
|
||||
* @see org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests
|
||||
*/
|
||||
@TestExecutionListeners(listeners = { ServletTestExecutionListener.class,
|
||||
DirtiesContextBeforeModesTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
|
||||
@TestExecutionListeners(listeners = { ServletTestExecutionListener.class, DirtiesContextBeforeModesTestExecutionListener.class,
|
||||
ApplicationEventsTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,
|
||||
DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class,
|
||||
SqlScriptsTestExecutionListener.class, EventPublishingTestExecutionListener.class }, inheritListeners = false)
|
||||
@Transactional
|
||||
|
||||
Reference in New Issue
Block a user