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

BEFORE_* modes in @DirtiesContext cause context to be closed before test [SPR-13180] #17772

Closed
spring-issuemaster opened this issue Jul 1, 2015 · 1 comment
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Jul 1, 2015

Sam Brannen opened SPR-13180 and commented

Status Quo

#17035 introduced BEFORE_METHOD, BEFORE_EACH_TEST_METHOD, and BEFORE_CLASS modes in @DirtiesContext. In Spring Framework 4.2 RC1, the DirtiesContextTestExecutionListener was updated to support these new BEFORE_\* modes in addition to the existing AFTER_\* modes.

However, there is a problem with having DirtiesContextTestExecutionListener support BEFORE_\* modes since it is configured (by default) to execute after the DependencyInjectionTestExecutionListener. This leads to several undesired side effects:

  1. The test's ApplicationContext is closed by the DirtiesContextTestExecutionListener after dependencies have been injected into the test instance.
  2. Injected dependencies may therefore attempt to interact with an ApplicationContext that is no longer active.
  3. If a test has its ApplicationContext injected as a dependency, interaction with the context will likely fail since the context has been closed.
  4. Furthermore, any TestExecutionListeners registered after the DirtiesContextTestExecutionListener will get a new ApplicationContext if they invoke TestContext.getApplicationContext().

Deliverables

  1. Introduce a new TestExecutionListener dedicated to handling BEFORE_\* modes in @DirtiesContext.
  2. Remove the support for BEFORE_\* modes from DirtiesContextTestExecutionListener.
  3. Update all supporting code to ensure that the new TestExecutionListener is registered before the DependencyInjectionTestExecutionListener (by default).

Affects: 4.2 RC1

Issue Links:

  • #17035 Introduce BEFORE_METHOD and BEFORE_CLASS modes for @DirtiesContext

Referenced from: commits 0aac02d

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Jul 1, 2015

Sam Brannen commented

Fixed in GitHub commit 0aac02d:

Introduce DirtiesContextBeforeModesTestExecutionListener

#17035 introduced various BEFORE_\* modes in @DirtiesContext. To support these new modes, DirtiesContextTestExecutionListener (DCTEL) was updated to support both BEFORE_\* and AFTER_\* modes. However, there is a problem with having DCTEL support BEFORE_\* modes since it is typically configured to execute after the DependencyInjectionTestExecutionListener (DITEL), and this leads to several undesired side effects:

  • The test's ApplicationContext is closed by DCTEL after dependencies have been injected into the test instance.
  • Injected dependencies may therefore attempt to interact with an ApplicationContext that is no longer active.
  • If a test has its ApplicationContext injected as a dependency, interaction with the context will likely fail since the context has been closed.
  • Any TestExecutionListeners registered after DCTEL will get a new ApplicationContext if they invoke getApplicationContext() on the TestContext.

This commit fixes these issues by introducing a new DirtiesContextBeforeModesTestExecutionListener (DCBMTEL) that is registered by default before DITEL. The previous support for BEFORE_\* modes has been moved from DCTEL to DCBMTEL. In addition, an AbstractDirtiesContextTestExecutionListener has been extracted from DCTEL in order to avoid code duplication.

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.