Skip to content

Commit

Permalink
fabric8io#2320 Prevent from closing client in afterEach for static Ku…
Browse files Browse the repository at this point in the history
…bernetesClient
  • Loading branch information
piomin committed Jul 1, 2020
1 parent bce858f commit 995ac41
Showing 1 changed file with 23 additions and 4 deletions.
Expand Up @@ -43,14 +43,18 @@ public class KubernetesMockServerExtension implements AfterEachCallback, AfterAl

@Override
public void afterEach(ExtensionContext context) throws Exception {
mock.destroy();
client.close();
Optional<Class<?>> optClass = context.getTestClass();
if (optClass.isPresent()) {
Class<?> testClass = optClass.get();
if (findField(testClass, true) == null) {
destroy();
}
}
}

@Override
public void afterAll(ExtensionContext context) throws Exception {
mock.destroy();
client.close();
destroy();
}

@Override
Expand Down Expand Up @@ -93,4 +97,19 @@ private void createKubernetesClient(Class<?> testClass) {
client = mock.createClient();
}

private void destroy() {
mock.destroy();
client.close();
}

private Field findField(Class<?> testClass, boolean isStatic) {
Field[] fields = testClass.getDeclaredFields();
for (Field f : fields) {
if (f.getType() == KubernetesClient.class && Modifier.isStatic(f.getModifiers()) == isStatic) {
return f;
}
}
return null;
}

}

0 comments on commit 995ac41

Please sign in to comment.