SLING-8331 osgi-mock: JUnit 5 Extension - Support setting context member field in super classes
diff --git a/junit5/src/main/java/org/apache/sling/testing/mock/osgi/junit5/OsgiContextExtension.java b/junit5/src/main/java/org/apache/sling/testing/mock/osgi/junit5/OsgiContextExtension.java
index 608238f..ca86f66 100644
--- a/junit5/src/main/java/org/apache/sling/testing/mock/osgi/junit5/OsgiContextExtension.java
+++ b/junit5/src/main/java/org/apache/sling/testing/mock/osgi/junit5/OsgiContextExtension.java
@@ -114,10 +114,19 @@
     }
 
     private Field getFieldFromTestInstance(Object testInstance, Class<?> type) {
-        Field contextField = Arrays.stream(testInstance.getClass().getDeclaredFields())
+        return getFieldFromTestInstance(testInstance.getClass(), type);
+    }
+
+    private Field getFieldFromTestInstance(Class<?> instanceClass, Class<?> type) {
+        if (instanceClass == null) {
+            return null;
+        }
+        Field contextField = Arrays.stream(instanceClass.getDeclaredFields())
                 .filter(field -> type.isAssignableFrom(field.getType())).findFirst().orElse(null);
         if (contextField != null) {
             contextField.setAccessible(true);
+        } else {
+            return getFieldFromTestInstance(instanceClass.getSuperclass(), type);
         }
         return contextField;
     }
diff --git a/junit5/src/test/java/org/apache/sling/testing/mock/osgi/junit5/OsgiContextMemberSuperClassTest.java b/junit5/src/test/java/org/apache/sling/testing/mock/osgi/junit5/OsgiContextMemberSuperClassTest.java
new file mode 100644
index 0000000..97ab38f
--- /dev/null
+++ b/junit5/src/test/java/org/apache/sling/testing/mock/osgi/junit5/OsgiContextMemberSuperClassTest.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.sling.testing.mock.osgi.junit5;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Test with OsgiContext member field in super class.
+ */
+@ExtendWith(OsgiContextExtension.class)
+class OsgiContextMemberSuperClassTest extends OsgiContextMemberTest {
+
+    @Test
+    void testSimpleService() {
+        context.registerService(Integer.class, new Integer(5));
+        
+        Integer service = context.getService(Integer.class);
+        assertEquals((Integer)5, service);
+    }
+
+}