When I have the jenvtest-fabric8-client-support dependency and have a test without a static KubernetesClient client field because I'm creating the client myself in this particular test, then the cleanup of the test fails with a NullpointerException:
java.lang.NullPointerException: Cannot invoke "io.fabric8.kubernetes.client.KubernetesClient.close()" because "this.client" is null
at io.javaoperatorsdk.jenvtest.junit.Fabric8ClientInjectionHandler.cleanup(Fabric8ClientInjectionHandler.java:48)
[...]
This is caused by the following.
When initializing, the injectionHandlers are only called when the field is available:
private void initialize(ExtensionContext extensionContext, boolean staticContext) {
var targetInjectors = clientInjectionHandlers.stream()
.filter(h -> h.isTargetFieldAvailable(extensionContext, staticContext)) // Filtered <----
.collect(Collectors.toList());
startIfAnnotationPresent(extensionContext, !targetInjectors.isEmpty());
targetInjectors.forEach(i -> i.inject(extensionContext, staticContext, kubeApiServer));
}
But when stopping, the injectionHandlers are always called (without check if the field is available):
private void stopIfAnnotationPresent(ExtensionContext extensionContext) {
extensionContext.getElement().ifPresent(ae -> {
var annotation = getExtensionAnnotationInstance(ae);
annotation.ifPresent(a -> {
clientInjectionHandlers.forEach(ih -> ih.cleanup(extensionContext)); // Not Filtered <----
kubeApiServer.stop();
});
});
}
This could easily be fixed by adding a null check in the cleanup method:
@Override
public void cleanup(ExtensionContext extensionContext) {
client.close();
}
Or by filtering the clientInjectionHandlers on isTargetFieldAvailable when calling the cleanup method.
Probably best both.
I can create a PR to fix this issue.
When I have the
jenvtest-fabric8-client-supportdependency and have a test without astatic KubernetesClient clientfield because I'm creating the client myself in this particular test, then the cleanup of the test fails with aNullpointerException:This is caused by the following.
When initializing, the injectionHandlers are only called when the field is available:
But when stopping, the injectionHandlers are always called (without check if the field is available):
This could easily be fixed by adding a null check in the cleanup method:
Or by filtering the clientInjectionHandlers on
isTargetFieldAvailablewhen calling thecleanupmethod.Probably best both.
I can create a PR to fix this issue.