-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Publish test execution events to the test ApplicationContext [SPR-13916] #18490
Comments
Frank Scheffler commented In fact, one solution would be to provide an additional EventPublishingTestExecutionListener, so users could plug-in the event publishing behavior, if they are in need for it. TestExecutionListener could look like this, backed by an event class hierarchy reflecting the testcontext lifecycle: public class EventPublishingTestExecutionListener extends AbstractTestExecutionListener {
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
testContext.getApplicationContext().publishEvent(new AfterMethodTestExecutionEvent(testContext));
}
} Within the test configuration code you could then, for instance, reset the mocks in question, e.g. by: @Configuration
public class MyMockConfiguration implements ApplicationListener<AfterMethodTestExecutionEvent> {
@Bean
public MyService myService() {
return Mockito.mock(MyService.class);
}
@Override
public void onApplicationEvent(AfterMethodTestExecutionEvent event) {
Mockito.reset(myService());
}
} |
Frank Scheffler commented I have created an implementation draft for further discussions at: maverick1601@f07c34c The tests work fine and I added EventListener-based meta-annotation to simplify integration into configuration classes and beans, e.g. simply annotating methods with I am, however, currently unsure about the following:
Finally I would like to summarize the benefits of this approach and why we need it:
|
Frank Scheffler commented Any feedback on this or the related question on Google groups (https://groups.google.com/forum/#!topic/spring-framework-contrib/crdJWZk3Wcs). I would like to proceed on finishing the code, if this is considered a useful enhancement to the TCF. |
Sam Brannen commented Thanks for the proposal and draft implementation. At a quick glance, it looks reasonably sound, though I haven't yet had time to look at it in great detail. With regard to the automatic registration of I'll move this issue to the 4.3 backlog with the hope of finding more time to devote to it as we move toward 4.3 GA. Regards, Sam |
Sam Brannen commented Please note that this issue is very closely related to and potentially supersedes #13352. |
Phil Webb commented You might be interested in spring-projects/spring-boot#5042 which is looking to add better Mockito support to Spring Boot. |
Frank Scheffler commented We have implemented this at a customer and enhanced the listener to publish events to the fixture, i.e. the test class, itself as well, if implemented as ApplicationListener or via |
This issue supersedes gh-13352. |
Related discussion on StackOverflow |
Hi @maverick1601,
I took a glance at your draft, and it looks very similar to solutions I've implemented for clients. Are you still interested in submitting a PR? If so, feel free to simply submit your draft, and I'll take it from there. Otherwise, I'll likely just implement it from scratch. I'm hoping to get this into Spring 5.2 M1, so I'd be grateful for prompt feedback. Thanks, Sam |
I would do so. Since the issue is about 3 yrs old, I don't know how much I would have to rewrite. Back then we could not fully agree on the fact, that such a TestExecutionListener would be included in the list of default listeners, especially with respect to the fact that such an event publishing TestExecutionListener would trigger context initialization. What is your opinion on that? |
I like to give credit where credit is due, so that would be great! Can you provide an estimate of when you think you'll able to do that?
I don't think you'd have to rewrite much of anything. There are two new methods in the
I'm currently inclined not to register this new listener by default. I think it's perhaps best to keep it as an opt-in feature. In any case, that can be decided later or changed before 5.2 GA is released. My main objective right now is to get the feature in place in 5.2 M1 so that people can experiment with it and provide feedback. Cheers, Sam p.s. don't worry so much about the documentation at this point unless you have time. If you don't have time, I'll pick up the documentation task after merging in your work. |
Okay, I took a look at it and it seems the main work is to rebase my previous proposal against master branch and to cover those two new life-cycle methods. I'd say this is done by the end of this week. |
Sounds good. Looking forward to your PR! |
@maverick1601, thanks for the PR. I'll take a look. |
Adds new TestExecutionListener for publishing events to the test application context. These may be consumed by @eventlistener annotated methods to react to the test life-cycle. The test execution listener isn't enabled by default. Implements spring-projects#18490
This commit introduces support for SpEL expressions for conditional event processing in annotations such as @BeforeTestClass, @BeforeTestMethod, etc. This commit also introduces a new getTestContext() method in TestContextEvent in order to improve the readability of such SpEL expressions. See gh-18490
Update: commit e272e2e introduced support for conditional event processing in the built-in event listener annotations. |
This commit introduces tests for both synchronous and asynchronous exception handling for TestContext event annotations. See gh-18490
This commit updates the class-level Javadoc for EventPublishingTestExecutionListener in order to provide explicit documentation for exception handling and async support. See gh-18490
Update: This new feature set is now documented in the Reference Manual. In addition, the |
Reopening to sort out issues with having the new The current state of affairs leads to undesirable side effects, such as:
|
Update: the implementation of this feature has been revised. See 353e092 for details. |
Frank Scheffler opened SPR-13916 and commented
We are using Mockito mocks quite often throughout our Spring-based integration tests, which leaves us with the problem of resetting them before/after each test method. While we can use some globally defined custom TestExecutionListener to reset all the mocks found within the ApplicationContext, it would be more practical to be able to define specific event-listeners within the application context itself. This would allow for more fine-grained control over which beans (i.e. mocks) should be reset.
Deliverables
EventPublishingTestExecutionListener
.TestContextEvent
and all concrete subclasses.@EventListener
annotations for all events.Issue Links:
2 votes, 6 watchers
The text was updated successfully, but these errors were encountered: