-
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
Close all ApplicationContexts in the TestContext framework after all tests have been executed #26196
Comments
That's an interesting use case, and we certainly don't have any features in place to support it.
I agree: one should definitely not annotate all test classes with
I took a look at the proposed
Ideally, there would be a general purpose mechanism for closing all contexts in the cache. The |
Has there been any progress on this issue? We are using RedisMessageListenerContainers that do not like Redis being closed prematurely and thus spam the log. |
I also wonder if there is any progress on this issue. 🤔 This also caused the following issues: |
For anyone relying on the aforementioned record ContextClosingResource(TestContextManager testContextManager) implements CloseableResource {
@Override
public void close() {
TestContext testContext = this.testContextManager.getTestContext();
if (testContext.hasApplicationContext()) {
testContext.markApplicationContextDirty(HierarchyMode.EXHAUSTIVE);
}
}
} Note that there is also no need to manually close the |
I wanted to bump this ticket. I'm also looking to solution to a similar problem with testcontainers, where the Spring context is not closed before the testcontainers are shutdown which spams our log output. |
When using
@SpringBootTest
with JUnit Jupiter a pitest user had a problem when executing tests with pitest (see hcoles/pitest#827). As far as my analysis brought me the cause is Spring Frameworks TestContext caching.Spring caches all application contexts loaded in a static variable and reused them among different tests and test classes (
TestContextManager
). Unfortunately Spring does not cleanup after all(!) tests have been run assuming the JVM will be terminated immediately after tests have been executed (which in turn triggers the automatic closing behavior for application contexts). This might hold for surefire or IDE starting those tests. But Pitest reuses the JVM instance for different test suite executions (multiple invocations of JUnit Jupiter'sLauncher.execute()
inside the same JVM) . Thus the cached application contexts stay there and interfere with future test runs.On the other hand Testcontainers integrates with the JUnit Jupiter Engine and uses ClosableResource hook to cleanup containers afterwards.
I've created a JUnit extension cleaning up application contexts on shutdown as a proof of concept. See SpringBootCleanup in the sample project originally provided by @jaguado-arima .
@DirtiesContext will have the same effect here and I didn't notice any differences in performance regarding this simple example. But I think it's a bad idea to add
@DirtiesContext
to all test classes only to be able to use pitest... if you're relying on application context caching to reduce overall testing time for a huge test suite adding@DirtiesContext
to all tests for the sake of mutation testing will slow down test execution (Springs context cache has been implemented for a reason...).@sbrannen If you consider changing the behavior as suggested I can also provide a pull request implementing the necessary changes for
SpringExtension
.The text was updated successfully, but these errors were encountered: