Skip to content

Commit

Permalink
#7: Implement fallback for empty ExtensionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneLimburg committed Apr 26, 2024
1 parent 426f5c5 commit a103fee
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/main/java/rocks/limburg/cdimock/CdiMockExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,27 @@
public class CdiMockExtension implements Extension {

private static ThreadLocal<ExtensionContext> beforeAllContext = new ThreadLocal<ExtensionContext>();
private static ExtensionContext latestExtensionContext;

private Set<Class<?>> excludedClasses;
private Set<Class<? extends Annotation>> excludingAnnotationTypes;
private Map<Class<?>, Object> mockitoBeans;

public static void beforeAll(ExtensionContext context) {
beforeAllContext.set(context);
latestExtensionContext = context;
}

public static void afterAll(ExtensionContext context) {
ExtensionContext beforeAllContext = beforeAllContext.get();
beforeAllContext.remove();
if (beforeAllContext == latestExtensionContext) {
latestExtensionContext = null;
}
}

public Optional<ExtensionContext> getExtensionContext() {
return ofNullable(beforeAllContext.get());
return ofNullable(beforeAllContext.get()).or(() -> ofNullable(latestExtensionContext));
}

public void excludeClasses(@Observes ProcessAnnotatedType<?> event) {
Expand Down

0 comments on commit a103fee

Please sign in to comment.