When a JUnit 5 test class is annotated with @EnabledIf and @DirtiesContext with @EnabledIf's property loadContext set to true, if its expression evaluates to false, 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.
When a JUnit 5 test class is annotated with
@EnabledIfand@DirtiesContextwith@EnabledIf's propertyloadContextset totrue, if its expression evaluates tofalse, the context loaded through the annotation is not cleaned up afterwards.If the expression evaluates to
trueand the tests are actually run, the context is cleaned up correctly.A workaround is to put
@EnabledIfon each test method, instead of the class.I ran into this issue running Kafka container tests conditionally, when Kafka consumers were created through
@EnableAutoConfigurationand were left over after the test was skipped. They were consuming messages of other tests that would run afterwards, breaking those tests.