|
4 | 4 | import org.junit.jupiter.api.extension.ExtensionContext; |
5 | 5 | import org.junit.jupiter.api.extension.TestInstancePostProcessor; |
6 | 6 |
|
| 7 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 8 | + |
7 | 9 | /** |
8 | 10 | * @author neo |
9 | 11 | */ |
10 | 12 | public final class IntegrationExtension implements TestInstancePostProcessor { |
11 | | - private static final String KEY_INITIALIZED = "initialized"; |
| 13 | + private static final AtomicBoolean INITIALIZED = new AtomicBoolean(false); |
12 | 14 |
|
13 | 15 | @Override |
14 | 16 | public void postProcessTestInstance(Object testInstance, ExtensionContext context) { |
15 | 17 | ExtensionContext.Store store = context.getRoot().getStore(ExtensionContext.Namespace.GLOBAL); |
16 | 18 | Class<?> testClass = context.getRequiredTestClass(); |
17 | | - AbstractTestModule module = store.computeIfAbsent(AbstractTestModule.class, _ -> createTestModule(testClass, store), AbstractTestModule.class); |
| 19 | + AbstractTestModule module = store.computeIfAbsent(AbstractTestModule.class, _ -> createTestModule(testClass), AbstractTestModule.class); |
18 | 20 | module.inject(testInstance); |
19 | 21 | } |
20 | 22 |
|
21 | | - private AbstractTestModule createTestModule(Class<?> testClass, ExtensionContext.Store store) { |
22 | | - Boolean initialized = store.get(KEY_INITIALIZED, Boolean.class); |
23 | | - if (Boolean.TRUE.equals(initialized)) throw new Error("test context failed to initialize, please check error message from previous integration test"); |
24 | | - store.computeIfAbsent(KEY_INITIALIZED, _ -> Boolean.TRUE, Boolean.class); |
| 23 | + private AbstractTestModule createTestModule(Class<?> testClass) { |
| 24 | + boolean initialized = INITIALIZED.get(); // junit 6 store doesn't support nested update, use global atomic to check loosely in test env |
| 25 | + if (initialized) throw new Error("test context failed to initialize, please check error message from previous integration test"); |
| 26 | + INITIALIZED.set(true); |
25 | 27 |
|
26 | 28 | Context context = findContext(testClass); |
27 | 29 | try { |
|
0 commit comments