Skip to content

Commit

Permalink
Find Bean Classes In '@Inject'-ed Instance<BeanType> Field
Browse files Browse the repository at this point in the history
Fixes #115
  • Loading branch information
philippkunz authored Jan 11, 2021
1 parent 96f3dbe commit d29ae4b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion junit5/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class MyTest {

## WeldJunit5AutoExtension

To use this approach, annotate your test class with `ExtendWith(WeldJunit5AutoExtension.class)` or just `@EnableAutoWeld`.
To use this approach, annotate your test class with `@ExtendWith(WeldJunit5AutoExtension.class)` or just `@EnableAutoWeld`.
By default, the extension will:

* Inspect your test class and try to figure out what bean classes it needs based on injection points (field and parameter injection both work)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.decorator.Decorator;
import javax.enterprise.context.Dependent;
import javax.enterprise.context.NormalScope;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.Stereotype;
import javax.enterprise.inject.spi.Extension;
Expand Down Expand Up @@ -95,7 +96,7 @@ static void scanForRequiredBeanClasses(List<Class<?>> testClasses, Weld weld, bo
.forEach(excludedBeanTypes::add);

AnnotationSupport.findAnnotatedFields(currClass, Inject.class).stream()
.map(Field::getType)
.map(ClassScanning::unwrapInstanceTypeParameter)
.forEach(cls -> addClassesToProcess(classesToProcess, cls));

AnnotationSupport.findAnnotatedMethods(currClass, Inject.class, HierarchyTraversalMode.BOTTOM_UP).stream()
Expand Down Expand Up @@ -330,4 +331,18 @@ private static Optional<Constructor<?>> findFirstAnnotatedConstructor(Class<?> c
return findFirstAnnotatedConstructor(clazz.getSuperclass(), annotationType);
}

private static Class<?> unwrapInstanceTypeParameter(Field field) {
Class<?> type = field.getType();
if (type.equals(Instance.class)) {
ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
Type typeParameter = parameterizedType.getActualTypeArguments()[0];
if (typeParameter instanceof ParameterizedType) {
type = (Class<?>) ((ParameterizedType) typeParameter).getRawType();
} else {
type = (Class<?>) typeParameter;
}
}
return type;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jboss.weld.junit5.auto;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.junit.jupiter.api.Test;

@EnableAutoWeld
public class InjectInstanceTest {

@Dependent
static class Foo {
}

@Inject
Instance<Foo> fooInstance;

@Test
void test() {
assertNotNull(fooInstance.get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jboss.weld.junit5.auto;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import org.junit.jupiter.api.Test;

@EnableAutoWeld
public class InjectParameterizedInstanceTest {

@Dependent
static class Foo<T> {
}

@Inject
Instance<Foo<String>> fooInstance;

@Test
void test() {
assertNotNull(fooInstance.get());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.jboss.weld.junit5.auto;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.enterprise.context.Dependent;
import javax.inject.Inject;

import org.junit.jupiter.api.Test;

@EnableAutoWeld
public class InjectParameterizedTest {

@Dependent
static class Foo<T> {
}

@Inject
Foo<String> foo;

@Test
void test() {
assertNotNull(foo);
}

}

0 comments on commit d29ae4b

Please sign in to comment.