Skip to content
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

Document effect of @DirtiesContext when used with constructor injection [SPR-17654] #22183

Closed
spring-projects-issues opened this issue Jan 9, 2019 · 1 comment
Assignees
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

@spring-projects-issues spring-projects-issues commented Jan 9, 2019

Sam Brannen opened SPR-17654 and commented

Overview

A recent discussion in the #spring channel on Kotlin Slack pointed out that issues arise when using @DirtiesContext to evict an ApplicationContext from the cache between test methods if the test class is executed with JUnit Jupiter and @TestInstance(PER_CLASS) semantics.

Analysis

Constructor injection for Spring-managed components will not work properly if @DirtiesContext is used to evict the test's ApplicationContext before or after test methods.

The reason is that the test instance will retain references to beans that came from a (now) closed ApplicationContext, and dependency injection will not occur again (to reset the references) since the constructor will only be invoked once.

To use @DirtiesContext before or after methods, one must allow dependencies from Spring to be injected via fields or setter/configuration methods.

Deliverables

  • Document this in the Testing chapter of the reference manual

Affects: 5.0.12

Referenced from: commits bd9d333, cf56506

@spring-projects-issues
Copy link
Collaborator Author

@spring-projects-issues spring-projects-issues commented Jan 9, 2019

Sam Brannen commented

This has been documented in the following commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.