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

fix: JUnit5 Kubernetes Extension works with Nested tests #5712

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,28 @@ default ExtensionContext.Store getStore(ExtensionContext context) {

default Field[] extractFields(ExtensionContext context, Class<?> clazz, Predicate<Field>... predicates) {
final List<Field> 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<Field> extractFields(Class<?> testClass, Class<?> clazz, Predicate<Field>... predicates) {
final List<Field> 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<Field> extractFields(Class<?> classWhereFieldIs, Class<?> fieldType,
/* private */static List<Field> extractFieldsFromClass(Class<?> classWhereFieldIs, Class<?> fieldType,
Predicate<Field>... predicates) {
if (classWhereFieldIs != null && classWhereFieldIs != Object.class) {
Stream<Field> fieldStream = Arrays.stream(classWhereFieldIs.getDeclaredFields())
Expand All @@ -68,14 +81,22 @@ default void setFieldValue(Field field, Object entity, Object value) throws Ille
}

default <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotation) {
T ret = null;
if (clazz != null) {
// Current
if (clazz.isAnnotationPresent(annotation)) {
return clazz.getAnnotation(annotation);
} else if (clazz.getSuperclass() != null) {
return findAnnotation(clazz.getSuperclass(), annotation);
ret = clazz.getAnnotation(annotation);
}
// Superclass
if (ret == null && clazz.getSuperclass() != null) {
ret = findAnnotation(clazz.getSuperclass(), annotation);
}
// Enclosing
if (ret == null && clazz.getEnclosingClass() != null) {
ret = findAnnotation(clazz.getEnclosingClass(), annotation);
}
}
return null;
return ret;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Client>) field.getType()));
for (Object testInstance : context.getRequiredTestInstances().getAllInstances()) {
if (field.getDeclaringClass().isAssignableFrom(testInstance.getClass())) {
setFieldValue(field, testInstance, getClient(context).adapt((Class<Client>) field.getType()));
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

}
}

}
Loading