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

Improve documentation for TestContext events #27757

Closed
biergit opened this issue Dec 1, 2021 · 4 comments
Closed

Improve documentation for TestContext events #27757

biergit opened this issue Dec 1, 2021 · 4 comments
Assignees
Labels
in: test Issues in the test module type: documentation A documentation task
Milestone

Comments

@biergit
Copy link

biergit commented Dec 1, 2021

Hi,

The BeforeTestClassEvent is not published by the EventPublishingTestExecutionListener since hasApplicationContext() returns false.

See here for a reproducer.

The JavaDoc of BeforeTestClass only mentions that the EventPublishingTestExecutionListener must be registered as TestExecutionListener which it is by default (so I also suggest reworking the documentation as normally no @TestExecutionListeners annotation is needed).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 1, 2021
@rstoyanchev rstoyanchev added the in: test Issues in the test module label Dec 2, 2021
@rstoyanchev rstoyanchev added this to the Triage Queue milestone Dec 2, 2021
@sbrannen
Copy link
Member

sbrannen commented Dec 3, 2021

As stated in the Javadoc for EventPublishingTestExecutionListener:

Events are only published if the ApplicationContext has already been loaded.

This was a design decision aimed at preventing the ApplicationContext from being loaded unnecessarily or too early.

If you have code that relies on the BeforeTestClassEvent, you will need to ensure that the ApplicationContext has been loaded via a TestExecutionListener (likely a custom implementation) that is registered before the EventPublishingTestExecutionListener which implements beforeTestClass(TestContext testContext) and invokes testContext#getApplicationContext().

The JavaDoc of BeforeTestClass only mentions that the EventPublishingTestExecutionListener must be registered as TestExecutionListener which it is by default (so I also suggest reworking the documentation as normally no @TestExecutionListeners annotation is needed).

Good point.

In light of that, I'll repurpose this issue to improve the documentation.

@sbrannen sbrannen added type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 3, 2021
@sbrannen sbrannen modified the milestones: Triage Queue, 5.3.14 Dec 3, 2021
@sbrannen sbrannen changed the title BeforeTestClassEvent is not published Improve documentation for TestContext events Dec 3, 2021
@biergit
Copy link
Author

biergit commented Dec 3, 2021

Hi Sam, could you please also consider the problematic mix of @DirtiesContext and listening to test events?

I think this is not covered by any documentation as well.

@jhoeller jhoeller modified the milestones: 5.3.14, 5.3.15 Dec 14, 2021
@sbrannen sbrannen modified the milestones: 5.3.15, 5.3.16 Jan 12, 2022
@sbrannen sbrannen modified the milestones: 5.3.16, 5.3.17 Feb 16, 2022
@sbrannen
Copy link
Member

sbrannen commented Mar 6, 2022

If you have code that relies on the BeforeTestClassEvent, you will need to ensure that the ApplicationContext has been loaded via a TestExecutionListener (likely a custom implementation) that is registered before the EventPublishingTestExecutionListener which implements beforeTestClass(TestContext testContext) and invokes testContext#getApplicationContext().

For example, the following custom TestExecutionListener achieves that goal.

@Order(0)
public class EagerLoadingTestExecutionListener implements TestExecutionListener {
	@Override
	public void beforeTestClass(TestContext testContext) {
		testContext.getApplicationContext();
	}
}

sbrannen added a commit to sbrannen/spring-framework that referenced this issue Mar 12, 2022
@sbrannen
Copy link
Member

Hi Sam, could you please also consider the problematic mix of @DirtiesContext and listening to test events?

I added a note regarding that (as well as dedicated integration tests) in d9c22e6.

In addition, my initial commit for this issue (a2f02db) also briefly mentioned @DirtiesContext.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

5 participants