Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EnableKubernetesMockClient fails for @Nested junit5 classes #3032

Open
dlafreniere opened this issue Apr 22, 2021 · 7 comments
Open

EnableKubernetesMockClient fails for @Nested junit5 classes #3032

dlafreniere opened this issue Apr 22, 2021 · 7 comments

Comments

@dlafreniere
Copy link

A test class annotated with @EnableKubernetesMockClient will execute @BeforeAll for any nested classes as well, except it will fail to retrieve the annotation.

To Reproduce:

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;

@EnableKubernetesMockClient(crud = true)
class OuterTest {
  static KubernetesClient client;
  @Test
  void shouldPass() {
    assertTrue(true);
  }
  @Nested
  class NestedTest {
    @Test
    void shouldPass() {
      assertTrue(true);
    }
  }
}

Stack Trace:

Test ignored.

java.lang.NullPointerException
	at io.fabric8.kubernetes.client.server.mock.KubernetesMockServerExtension.initializeKubernetesClientAndMockServer(KubernetesMockServerExtension.java:88)
	at io.fabric8.kubernetes.client.server.mock.KubernetesMockServerExtension.setKubernetesClientAndMockServerFields(KubernetesMockServerExtension.java:123)
	at io.fabric8.kubernetes.client.server.mock.KubernetesMockServerExtension.beforeAll(KubernetesMockServerExtension.java:72)
	....
@manusa manusa added the bug label Apr 22, 2021
@rohanKanojia
Copy link
Member

I think it'll work if you specify @EnableKubernetesMockClient annotation like this:

@EnableKubernetesMockClient(crud = true)
class OuterTest {
  static KubernetesClient client;
  @Test
  void shouldPass() {
    assertTrue(true);
  }
  @Nested
  @EnableKubernetesMockClient(crud = true)
  class NestedTest {
    @Test
    void shouldPass() {
      assertTrue(true);
    }
  }
}

@dlafreniere
Copy link
Author

I think it'll work if you specify @EnableKubernetesMockClient annotation like this:

@EnableKubernetesMockClient(crud = true)
class OuterTest {
  static KubernetesClient client;
  @Test
  void shouldPass() {
    assertTrue(true);
  }
  @Nested
  @EnableKubernetesMockClient(crud = true)
  class NestedTest {
    @Test
    void shouldPass() {
      assertTrue(true);
    }
  }
}

Thanks, yea, I tried this as well. Although it doesn't crash, the client doesn't get cleaned up in the afterEach for the nested tests.

@rohanKanojia
Copy link
Member

Code for this extension is here: https://github.com/fabric8io/kubernetes-client/blob/master/kubernetes-server-mock/src/main/java/io/fabric8/kubernetes/client/server/mock/EnableKubernetesMockClient.java

Would it be possible for you to debug what's wrong here and submit a PR?

@wind57
Copy link
Contributor

wind57 commented May 22, 2021

@dlafreniere can you provide an actual sample of what you mean? You have the annotation @EnableKubernetesMockClient(crud = true) on the @Nested class, but you do not have neither the KubernetesMockServer nor the KubernetesClient in that nested class, so this is a bit confusing.

A sample of your exact use-case would help debug this further.

@stale
Copy link

stale bot commented Aug 20, 2021

This issue has been automatically marked as stale because it has not had any activity since 90 days. It will be closed if no further activity occurs within 7 days. Thank you for your contributions!

@rohanKanojia
Copy link
Member

I bumped into this issue while writing some test involving nested Junit5 class.

I've attached a reproducer project that's using Kubernetes Client v6.10.0

nested-kubernetes-mock-server-reproducer.zip

@manusa
Copy link
Member

manusa commented Jan 13, 2024

Sorry, yes I confirmed earlier this week that this was still failing.

Shouldn't be that hard to fix. We just need to toggle the part of the extension where it's setting the injected fields.

In any case, we might want to delay this change to after we completely analyze the more extensive MockWebServer changes (#5632).

@manusa manusa self-assigned this Jan 17, 2024
@manusa manusa removed their assignment Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants