From 1a37bf5c274d1d4bf78f2a3e80eaaf6e4dc66d9b Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Wed, 17 Jan 2024 11:33:19 +0100 Subject: [PATCH] fix: JUnit5 Kubernetes Extension works with Nested tests --- CHANGELOG.md | 1 + .../fabric8/junit/jupiter/BaseExtension.java | 21 ++++-- .../junit/jupiter/KubernetesExtension.java | 6 +- .../api/KubernetesTestWithNestedTest.java | 69 +++++++++++++++++++ 4 files changed, 92 insertions(+), 5 deletions(-) create mode 100644 junit/kubernetes-junit-jupiter/src/test/java/io/fabric8/junit/jupiter/api/KubernetesTestWithNestedTest.java diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b15678bc67..318cabd035b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### 6.11-SNAPSHOT #### Bugs +* Fix #3032: JUnit5 Kubernetes Extension works with Nested tests #### Improvements * Fix #5701: Owner reference validity check regarding scope and namespace diff --git a/junit/kubernetes-junit-jupiter/src/main/java/io/fabric8/junit/jupiter/BaseExtension.java b/junit/kubernetes-junit-jupiter/src/main/java/io/fabric8/junit/jupiter/BaseExtension.java index 8c5afaa4e9e..0ea474471c1 100644 --- a/junit/kubernetes-junit-jupiter/src/main/java/io/fabric8/junit/jupiter/BaseExtension.java +++ b/junit/kubernetes-junit-jupiter/src/main/java/io/fabric8/junit/jupiter/BaseExtension.java @@ -39,15 +39,28 @@ default ExtensionContext.Store getStore(ExtensionContext context) { default Field[] extractFields(ExtensionContext context, Class clazz, Predicate... predicates) { final List fields = new ArrayList<>(); - Class testClass = context.getTestClass().orElse(Object.class); - do { + if (context.getTestClass().isPresent()) { + Class testClass = context.getTestClass().orElse(Object.class); fields.addAll(extractFields(testClass, clazz, predicates)); + Class enclosingTestClass = testClass.getEnclosingClass(); + while (enclosingTestClass != null) { + fields.addAll(extractFields(enclosingTestClass, clazz, predicates)); + enclosingTestClass = enclosingTestClass.getEnclosingClass(); + } + } + return fields.toArray(new Field[0]); + } + + default List extractFields(Class testClass, Class clazz, Predicate... predicates) { + final List fields = new ArrayList<>(); + do { + fields.addAll(extractFieldsFromClass(testClass, clazz, predicates)); testClass = testClass.getSuperclass(); } while (testClass != Object.class); - return fields.toArray(new Field[0]); + return fields; } - /* private */static List extractFields(Class classWhereFieldIs, Class fieldType, + /* private */static List extractFieldsFromClass(Class classWhereFieldIs, Class fieldType, Predicate... predicates) { if (classWhereFieldIs != null && classWhereFieldIs != Object.class) { Stream fieldStream = Arrays.stream(classWhereFieldIs.getDeclaredFields()) diff --git a/junit/kubernetes-junit-jupiter/src/main/java/io/fabric8/junit/jupiter/KubernetesExtension.java b/junit/kubernetes-junit-jupiter/src/main/java/io/fabric8/junit/jupiter/KubernetesExtension.java index a9a840e4635..693c9bfd556 100644 --- a/junit/kubernetes-junit-jupiter/src/main/java/io/fabric8/junit/jupiter/KubernetesExtension.java +++ b/junit/kubernetes-junit-jupiter/src/main/java/io/fabric8/junit/jupiter/KubernetesExtension.java @@ -36,7 +36,11 @@ public void beforeAll(ExtensionContext context) throws Exception { @Override public void beforeEach(ExtensionContext context) throws Exception { for (Field field : extractFields(context, KubernetesClient.class, f -> !Modifier.isStatic(f.getModifiers()))) { - setFieldValue(field, context.getRequiredTestInstance(), getClient(context).adapt((Class) field.getType())); + for (Object testInstance : context.getRequiredTestInstances().getAllInstances()) { + if (field.getDeclaringClass().isAssignableFrom(testInstance.getClass())) { + setFieldValue(field, testInstance, getClient(context).adapt((Class) field.getType())); + } + } } } } diff --git a/junit/kubernetes-junit-jupiter/src/test/java/io/fabric8/junit/jupiter/api/KubernetesTestWithNestedTest.java b/junit/kubernetes-junit-jupiter/src/test/java/io/fabric8/junit/jupiter/api/KubernetesTestWithNestedTest.java new file mode 100644 index 00000000000..4a0bfee985e --- /dev/null +++ b/junit/kubernetes-junit-jupiter/src/test/java/io/fabric8/junit/jupiter/api/KubernetesTestWithNestedTest.java @@ -0,0 +1,69 @@ +/** + * Copyright (C) 2015 Red Hat, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.fabric8.junit.jupiter.api; + +import io.fabric8.kubernetes.client.KubernetesClient; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +@KubernetesTest(createEphemeralNamespace = false) +class KubernetesTestWithNestedTest { + + private static KubernetesClient staticKubernetesClient; + private KubernetesClient kubernetesClient; + + @Test + void staticKubernetesClient() { + assertThat(staticKubernetesClient).isNotNull(); + } + + @Test + void kubernetesClient() { + assertThat(kubernetesClient).isNotNull(); + } + + @Nested + class NestedTest { + + @Test + void staticKubernetesClient() { + assertThat(staticKubernetesClient).isNotNull(); + } + + @Test + void kubernetesClient() { + assertThat(kubernetesClient).isNotNull(); + } + + @Nested + class NestedNestedTest { + + @Test + void staticKubernetesClient() { + assertThat(staticKubernetesClient).isNotNull(); + } + + @Test + void kubernetesClient() { + assertThat(kubernetesClient).isNotNull(); + } + + } + } + +}