Skip to content

Commit

Permalink
Merge pull request quarkusio#35632 from manovotn/issue35581_part2
Browse files Browse the repository at this point in the history
Add minor clarifications and cleanup to CreateMockitoSpiesCallback
  • Loading branch information
mkouba authored Aug 30, 2023
2 parents 1c7152b + 98c57e1 commit 233fcf4
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.mockito.Mockito;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.ClientProxy;
import io.quarkus.arc.InjectableContext;
import io.quarkus.arc.InstanceHandle;
Expand All @@ -18,16 +19,18 @@

public class CreateMockitoSpiesCallback implements QuarkusTestAfterConstructCallback, QuarkusTestAfterAllCallback {

// in nested tests, there are multiple states created before destruction is triggered
// Set is here because in nested tests, there are multiple states created before destruction is triggered
// This field needs to be static because each implemented callback created a new instance of this class
private static Set<InjectableContext.ContextState> statesToDestroy = new HashSet<>();

@Override
public void afterConstruct(Object testInstance) {
Class<?> current = testInstance.getClass();
// QuarkusTestAfterConstructCallback can be used in @QuarkusIntegrationTest where there is no Arc
boolean contextPreviouslyActive = Arc.container() != null && Arc.container().requestContext().isActive();
ArcContainer container = Arc.container();
boolean contextPreviouslyActive = container != null && container.requestContext().isActive();
if (!contextPreviouslyActive) {
Arc.container().requestContext().activate();
statesToDestroy.add(container.requestContext().activate());
}
while (current.getSuperclass() != null) {
for (Field field : current.getDeclaredFields()) {
Expand All @@ -44,8 +47,7 @@ public void afterConstruct(Object testInstance) {
}
if (!contextPreviouslyActive) {
// only deactivate; we will destroy them in QuarkusTestAfterAllCallback
statesToDestroy.add(Arc.container().requestContext().getState());
Arc.container().requestContext().deactivate();
container.requestContext().deactivate();
}
}

Expand Down

0 comments on commit 233fcf4

Please sign in to comment.