英文:
Quarkus BeanManager with unit test
问题
BeanManager在上述用例中的单元测试中没有返回bean。该项目是一个Java库。
Bean接口
public interface My {
String turn();
}
Bean限定符
@Qualifier
@Target({ ElementType.TYPE, ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface My1 {
}
Bean类
@ApplicationScoped
@My1
public class MyClass1 implements My {
@Override
public String turn() {
return "1";
}
}
以下单元测试的beans列表为空而失败。
@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));
}
}
test1和test2的JUnit输出
java.lang.AssertionError:
Expected: is <false>
but: was <true>
Expected :is <false>
Actual :<true>
在另一个Java库项目中运行相同示例正常工作。
在单元测试类中添加一个注入的My属性也正常工作。
可能出了什么问题?
在这个例子中,BeanManager出了什么问题?
谢谢
英文:
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
答案1
得分: 3
很有可能您的 bean 被视为未使用,并在构建过程中被移除。详见 https://quarkus.io/guides/cdi-reference#remove_unused_beans 获取更多信息。
您可以尝试使用@Unremovable
为您的 bean 添加注解。
另外请注意,BeanManager
不应该被应用程序代码使用。它是一个集成 SPI。此外,Quarkus 提供了更符合习惯的方式来集成第三方库和框架。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论