diff --git a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java index 5b26337f7966d..1aba39aa6beb5 100644 --- a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java +++ b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java @@ -8,10 +8,14 @@ import org.junit.jupiter.api.extension.ExtendWith; +import io.quarkus.test.InjectMock; import io.smallrye.common.annotation.Experimental; /** * Registers the {@link QuarkusComponentTestExtension} that makes it easy to test Quarkus components. + * + * @see InjectMock + * @see TestConfigProperty */ @Experimental("This feature is experimental and the API may change in the future") @ExtendWith(QuarkusComponentTestExtension.class) diff --git a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java index 51e3bec34d887..865fb1b31a398 100644 --- a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java +++ b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java @@ -124,6 +124,9 @@ * configuration properties via the {@link #configProperty(String, String)} method. If you only need to use the default values * for missing config properties, then the {@link #useDefaultConfigProperties()} * might come in useful. + * + * @see InjectMock + * @see TestConfigProperty */ @Experimental("This feature is experimental and the API may change in the future") public class QuarkusComponentTestExtension @@ -195,7 +198,7 @@ public QuarkusComponentTestExtension configProperty(String key, String value) { } /** - * Use the default values for missing config properties. By default, if missing config property results in a test failure. + * Use the default values for missing config properties. By default, a missing config property results in a test failure. *

* For primitives the default values as defined in the JLS are used. For any other type {@code null} is injected. * @@ -246,10 +249,14 @@ public void beforeAll(ExtensionContext context) throws Exception { } } // All fields annotated with @Inject represent component classes - for (Field field : testClass.getDeclaredFields()) { - if (field.isAnnotationPresent(Inject.class) && !resolvesToBuiltinBean(field.getType())) { - componentClasses.add(field.getType()); + Class current = testClass; + while (current != null) { + for (Field field : current.getDeclaredFields()) { + if (field.isAnnotationPresent(Inject.class) && !resolvesToBuiltinBean(field.getType())) { + componentClasses.add(field.getType()); + } } + current = current.getSuperclass(); } TestConfigProperty[] testConfigProperties = testClass.getAnnotationsByType(TestConfigProperty.class); @@ -761,13 +768,17 @@ private List injectFields(Class testClass, Object testInstance injectAnnotations = List.of(Inject.class, InjectMock.class); } List injectedFields = new ArrayList<>(); - for (Field field : testClass.getDeclaredFields()) { - for (Class annotation : injectAnnotations) { - if (field.isAnnotationPresent(annotation)) { - injectedFields.add(new FieldInjector(field, testInstance)); - break; + Class current = testClass; + while (current.getSuperclass() != null) { + for (Field field : current.getDeclaredFields()) { + for (Class annotation : injectAnnotations) { + if (field.isAnnotationPresent(annotation)) { + injectedFields.add(new FieldInjector(field, testInstance)); + break; + } } } + current = current.getSuperclass(); } return injectedFields; } diff --git a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/TestConfigProperty.java b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/TestConfigProperty.java index f493d669c2881..6bba48ac31209 100644 --- a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/TestConfigProperty.java +++ b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/TestConfigProperty.java @@ -10,7 +10,7 @@ import io.quarkus.test.component.TestConfigProperty.TestConfigProperties; /** - * Set a configuration property for the test. + * Set the value of a configuration property for a {@code io.quarkus.test.component.QuarkusComponentTest}. * * @see QuarkusComponentTest * @see QuarkusComponentTestExtension#configProperty(String, String) diff --git a/test-framework/junit5-component/src/test/java/io/quarkus/test/component/declarative/SuperClassInjectionTest.java b/test-framework/junit5-component/src/test/java/io/quarkus/test/component/declarative/SuperClassInjectionTest.java new file mode 100644 index 0000000000000..70f603ce77699 --- /dev/null +++ b/test-framework/junit5-component/src/test/java/io/quarkus/test/component/declarative/SuperClassInjectionTest.java @@ -0,0 +1,21 @@ +package io.quarkus.test.component.declarative; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import io.quarkus.test.component.QuarkusComponentTest; +import io.quarkus.test.component.TestConfigProperty; + +@QuarkusComponentTest +@TestConfigProperty(key = "foo", value = "BAR") +public class SuperClassInjectionTest extends SuperTest { + + @Test + public void testPing() { + Mockito.when(charlie.ping()).thenReturn("foo"); + assertEquals("foo and BAR", myComponent.ping()); + } + +} diff --git a/test-framework/junit5-component/src/test/java/io/quarkus/test/component/declarative/SuperTest.java b/test-framework/junit5-component/src/test/java/io/quarkus/test/component/declarative/SuperTest.java new file mode 100644 index 0000000000000..751b497c9cf85 --- /dev/null +++ b/test-framework/junit5-component/src/test/java/io/quarkus/test/component/declarative/SuperTest.java @@ -0,0 +1,17 @@ +package io.quarkus.test.component.declarative; + +import jakarta.inject.Inject; + +import io.quarkus.test.InjectMock; +import io.quarkus.test.component.beans.Charlie; +import io.quarkus.test.component.beans.MyComponent; + +public abstract class SuperTest { + + @Inject + MyComponent myComponent; + + @InjectMock + Charlie charlie; + +}