Skip to content

Commit

Permalink
Merge pull request #34059 from mkouba/componenttest-superclasses
Browse files Browse the repository at this point in the history
QuarkusComponentTest: collect injection points from superclasses
  • Loading branch information
mkouba authored Jun 15, 2023
2 parents bd18df7 + 2bcc5bb commit 974bc29
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
* <p>
* For primitives the default values as defined in the JLS are used. For any other type {@code null} is injected.
*
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -761,13 +768,17 @@ private List<FieldInjector> injectFields(Class<?> testClass, Object testInstance
injectAnnotations = List.of(Inject.class, InjectMock.class);
}
List<FieldInjector> injectedFields = new ArrayList<>();
for (Field field : testClass.getDeclaredFields()) {
for (Class<? extends Annotation> 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<? extends Annotation> annotation : injectAnnotations) {
if (field.isAnnotationPresent(annotation)) {
injectedFields.add(new FieldInjector(field, testInstance));
break;
}
}
}
current = current.getSuperclass();
}
return injectedFields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Original file line number Diff line number Diff line change
@@ -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;

}

0 comments on commit 974bc29

Please sign in to comment.