-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
DirtiesContextTestExecutionListener
has unexpected execution order leading to errors
#34225
Comments
spring-projectsgh-34225 Signed-off-by: Mengqi Xu <[email protected]>
Hi, You can find the # Default TestExecutionListeners for the Spring TestContext Framework
#
org.springframework.test.context.TestExecutionListener = ... and find this orders:
At design, I thinks this order magic values use to determine the relative order of At use, If you want add a new
You can set the order like 3001 (3000 < order < 3005), it will work like the relative order. Finally, I think It's necessary to add the comment for the method /**
* Returns {@code 1950}, which ensures that the {@code BeanOverrideTestExecutionListener}
* is ordered after the
* {@link org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener
* DirtiesContextBeforeModesTestExecutionListener} and just before the
* {@link DependencyInjectionTestExecutionListener}.
*/
@Override
public int getOrder() {
return 1950;
} |
This comment has been minimized.
This comment has been minimized.
DirtiesContextTestExecutionListener
has unexpected execution order leading to errors
Hi @tfactor2, I don't understand the basis for your claim that " All The Ordering TestExecutionListener Implementations section of the reference manual also states the following.
In light of the above, I am closing this issue as "works as designed". Regards, Sam |
Hi @remeio,
Indeed, that would be useful, and I will process PR #34265 that you submitted. 👍
Glad you like it! That was the last
FYI: That issue is related to aspect ordering (AOP) and is unrelated to |
@sbrannen, thanks for pointing that out.
public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception)
....
// Traverse the TestExecutionListeners in reverse order to ensure proper
// "wrapper"-style execution of listeners.
for (TestExecutionListener testExecutionListener : getReversedTestExecutionListeners()) {
|
Thanks for the proposals, @nizametdinovcrx. Feedback
I partially addressed that in commit 9d3374b, and I have raised #34422 to further improve the documentation.
I addressed that in #34404.
In any case, in the future we may have our own valid reasons to introduce a core "system" listener that does not have an order value in one of those ranges. In light of that, I don't think it warrants attempting to define a reserved range. Furthermore, those ranges are not actually reserved for core listeners, and third parties are actually encouraged to introduce listeners with order values within those ranges so that they can be positioned correctly. Related Issues |
@sbrannen, great, thx! |
We encountered an issue where our custom test execution listener, responsible for cleaning the database after-test, was executed after the ApplicationContext was cleaned up. We had set the listener order to
3000
to prevent conflicts with other internal listeners.However, we didn't know that
DirtiesContextTestExecutionListener
had the same order level (3000
), leading our cleanup listener to run after the context was marked dirty.This behavior led to unexpected results - Spring initiated a new context since the previous one was removed, resulting in the database being cleaned for another test.
Additionally, it would be great to understand the rationale behind
3000
forDirtiesContextTestExecutionListener
.Looks like the logic should be more complex:
after the method marking should be executed last (having listener with the lowest order value),
before the method marking should be executed first (listener with the highest order value).
Spring Version: 6.1.13
The text was updated successfully, but these errors were encountered: