mirror of
https://github.com/keycloak/keycloak.git
synced 2026-02-14 19:29:12 -06:00
Support injecting into superclass of tests (#35084)
Closes #35082 Signed-off-by: stianst <stianst@gmail.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import org.keycloak.test.framework.config.Config;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
@@ -136,7 +137,7 @@ public class Registry implements ExtensionContext.Store.CloseableResource {
|
||||
requestedInstances.add(requestedServerInstance);
|
||||
}
|
||||
|
||||
for (Field f : testClass.getDeclaredFields()) {
|
||||
for (Field f : listFields(testClass)) {
|
||||
RequestedInstance requestedInstance = createRequestedInstance(f.getAnnotations(), f.getType());
|
||||
if (requestedInstance != null) {
|
||||
requestedInstances.add(requestedInstance);
|
||||
@@ -192,7 +193,7 @@ public class Registry implements ExtensionContext.Store.CloseableResource {
|
||||
}
|
||||
|
||||
private void injectFields(Object testInstance) {
|
||||
for (Field f : testInstance.getClass().getDeclaredFields()) {
|
||||
for (Field f : listFields(testInstance.getClass())) {
|
||||
InstanceContext<?, ?> instance = getDeployedInstance(f.getType(), f.getAnnotations());
|
||||
if(instance == null) { // a test class might have fields not meant for injection
|
||||
continue;
|
||||
@@ -369,4 +370,16 @@ public class Registry implements ExtensionContext.Store.CloseableResource {
|
||||
}
|
||||
}
|
||||
|
||||
private List<Field> listFields(Class clazz) {
|
||||
List<Field> fields = new LinkedList<>(Arrays.asList(clazz.getDeclaredFields()));
|
||||
|
||||
Class<?> superclass = clazz.getSuperclass();
|
||||
while (superclass != null && !superclass.equals(Object.class)) {
|
||||
fields.addAll(Arrays.asList(superclass.getDeclaredFields()));
|
||||
superclass = superclass.getSuperclass();
|
||||
}
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.keycloak.test.examples;
|
||||
|
||||
import org.keycloak.test.framework.annotations.InjectRealm;
|
||||
import org.keycloak.test.framework.realm.ManagedRealm;
|
||||
|
||||
public abstract class AbstractTest {
|
||||
|
||||
@InjectRealm
|
||||
ManagedRealm realm;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.keycloak.test.examples;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.keycloak.test.framework.annotations.KeycloakIntegrationTest;
|
||||
|
||||
@KeycloakIntegrationTest
|
||||
public class InjectIntoAbstractFieldsTest extends AbstractTest {
|
||||
|
||||
@Test
|
||||
public void testManagedRealm() {
|
||||
Assertions.assertNotNull(realm);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user