Reputation: 81
BeanManager does not return beans during unit test in the use case describe above. The project is a Java library.
Bean interface
public interface My {
String turn();
}
Bean qualifier
@Qualifier
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface My1 {
}
Bean class
@ApplicationScoped
@My1
public class MyClass1 implements My {
@Override
public String turn() {
return "1";
}
}
The following unit test failed with empty beans list.
@QuarkusTest
public class MyTest {
@Test
public void test1() throws IllegalAccessException, InstantiationException {
Set<Bean<?>> beans = beanManager.getBeans(My.class, new AnnotationLiteral<My1>() {});
MatcherAssert.assertThat(beans.isEmpty(), Is.is(false));
}
@Test
public void test2() throws IllegalAccessException, InstantiationException {
// Class<? extends Annotation>
final Class<? extends Annotation> annotationClass = My1.class;
final Annotation qualifier = new Annotation() {
@Override
public Class<? extends Annotation> annotationType() {
return annotationClass;
}
};
Set<Bean<?>> beans = beanManager.getBeans(My.class, qualifier);
MatcherAssert.assertThat(beans.isEmpty(), Is.is(false));
}
JUnit output for test1 and test2
java.lang.AssertionError:
Expected: is <false>
but: was <true>
Expected :is <false>
Actual :<true>
Running the same sample in another Java library project works fine.
Adding an injected My attribute in the unit test class works fine too.
What could be wrong ? What goes wrong with BeanManager in this example ?
Thx
Upvotes: 0
Views: 1174
Reputation: 1526
It is very likely that your bean is considered unused and removed during the build. See https://quarkus.io/guides/cdi-reference#remove_unused_beans for more info.
You can try to annotate your bean with @Unremovable
.
Also note that BeanManager
is not meant to be used by the application code. It's an integration SPI. Moreover, Quarkus offers more idiomatic ways of integrating third party libraries and framework.
Upvotes: 3