forked from jaguado-arima/pitTestcontainers
/
SpringBootCleanup.java
45 lines (37 loc) · 1.95 KB
/
SpringBootCleanup.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.example.pitTestcontainers.pets;
import java.io.Closeable;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.ExtensionContext.Store;
import org.springframework.context.ApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextManager;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* @author Stefan Pennndorf
*/
public class SpringBootCleanup implements Extension, AfterAllCallback {
private static final Namespace SPRING_NAMESPACE = Namespace.create(SpringExtension.class);
private static final Namespace CLEANUP_NAMESPACE = Namespace.create(SpringBootCleanup.class);
@Override
public void afterAll(final ExtensionContext context) {
final Store store = context.getRoot().getStore(SPRING_NAMESPACE);
Class<?> testClass = context.getRequiredTestClass();
final TestContextManager testContextManager = store.get(testClass, TestContextManager.class);
final Store cleanupStore = context.getRoot().getStore(CLEANUP_NAMESPACE);
cleanupStore.getOrComputeIfAbsent(testClass, k -> closingTCM(testContextManager));
}
private Store.CloseableResource closingTCM(final TestContextManager testContextManager) {
final TestContext testContext = testContextManager.getTestContext();
return () -> {
final ApplicationContext applicationContext = testContext.getApplicationContext();
testContext.markApplicationContextDirty(DirtiesContext.HierarchyMode.EXHAUSTIVE);
if (applicationContext instanceof Closeable) {
((Closeable) applicationContext).close();
}
};
}
}