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
@DirtiesContext not applied when class-level @EnabledIf evaluates to false #26694
Comments
That sounds like an interesting combination of actors at play. I'll investigate if we can improve that. Thanks for raising the issue! |
Just to clarify, do you have a bare |
The reason you are experiencing this behavior is that a JUnit Jupiter So, to get this to work, we'll have to add some special logic to the |
I had tried all three options (bare, |
Thanks for the feedback. See #26694 (comment) for my analysis of why it doesn't work for all options. |
Prior to this commit, if a test class annotated with @DirtiesContext and @EnabledIf/@DisabledIf with `loadContext = true` was disabled due to the evaluated SpEL expression, the ApplicationContext would not be marked as dirty and closed. The reason is that @EnabledIf/@DisabledIf are implemented via JUnit Jupiter's ExecutionCondition extension API which results in the entire test class (as well as any associated extension callbacks) being skipped if the condition evaluates to `disabled`. This effectively prevents any of Spring's TestExecutionListener APIs from being invoked. Consequently, the DirtiesContextTestExecutionListener does not get a chance to honor the class-level @DirtiesContext declaration. This commit fixes this by implementing part of the logic of DirtiesContextTestExecutionListener in AbstractExpressionEvaluatingCondition (i.e., the base class for @EnabledIf/@DisabledIf support). Specifically, if the test class for an eagerly loaded ApplicationContext is disabled, AbstractExpressionEvaluatingCondition will now mark the test ApplicationContext as dirty if the test class is annotated with @DirtiesContext. Closes spring-projectsgh-26694
Wow, amazing response and resolution time. Thank you so much for acting so quickly! |
You're welcome. Please note that this has been fixed in Feel free to try it out in the latest snapshots for 5.3.6 and 5.2.14 and let us know if it works for you. |
Works for me with Spring Boot |
avoiding the Spring bug spring-projects/spring-framework#26694
avoiding the Spring bug spring-projects/spring-framework#26694
Prior to this commit, if a test class annotated with @DirtiesContext and @EnabledIf/@DisabledIf with `loadContext = true` was disabled due to the evaluated SpEL expression, the ApplicationContext would not be marked as dirty and closed. The reason is that @EnabledIf/@DisabledIf are implemented via JUnit Jupiter's ExecutionCondition extension API which results in the entire test class (as well as any associated extension callbacks) being skipped if the condition evaluates to `disabled`. This effectively prevents any of Spring's TestExecutionListener APIs from being invoked. Consequently, the DirtiesContextTestExecutionListener does not get a chance to honor the class-level @DirtiesContext declaration. This commit fixes this by implementing part of the logic of DirtiesContextTestExecutionListener in AbstractExpressionEvaluatingCondition (i.e., the base class for @EnabledIf/@DisabledIf support). Specifically, if the test class for an eagerly loaded ApplicationContext is disabled, AbstractExpressionEvaluatingCondition will now mark the test ApplicationContext as dirty if the test class is annotated with @DirtiesContext. Closes spring-projectsgh-26694
Prior to this commit, if a test class annotated with @DirtiesContext and @EnabledIf/@DisabledIf with `loadContext = true` was disabled due to the evaluated SpEL expression, the ApplicationContext would not be marked as dirty and closed. The reason is that @EnabledIf/@DisabledIf are implemented via JUnit Jupiter's ExecutionCondition extension API which results in the entire test class (as well as any associated extension callbacks) being skipped if the condition evaluates to `disabled`. This effectively prevents any of Spring's TestExecutionListener APIs from being invoked. Consequently, the DirtiesContextTestExecutionListener does not get a chance to honor the class-level @DirtiesContext declaration. This commit fixes this by implementing part of the logic of DirtiesContextTestExecutionListener in AbstractExpressionEvaluatingCondition (i.e., the base class for @EnabledIf/@DisabledIf support). Specifically, if the test class for an eagerly loaded ApplicationContext is disabled, AbstractExpressionEvaluatingCondition will now mark the test ApplicationContext as dirty if the test class is annotated with @DirtiesContext. Closes spring-projectsgh-26694
When a JUnit 5 test class is annotated with
@EnabledIf
and@DirtiesContext
with@EnabledIf
's propertyloadContext
set totrue
, if its expression evaluates tofalse
, the context loaded through the annotation is not cleaned up afterwards.If the expression evaluates to
true
and the tests are actually run, the context is cleaned up correctly.A workaround is to put
@EnabledIf
on each test method, instead of the class.I ran into this issue running Kafka container tests conditionally, when Kafka consumers were created through
@EnableAutoConfiguration
and were left over after the test was skipped. They were consuming messages of other tests that would run afterwards, breaking those tests.The text was updated successfully, but these errors were encountered: