【发布时间】:2020-04-08 20:00:06
【问题描述】:
在上述用例中,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 {
}
豆类
@ApplicationScoped
@My1
public class MyClass1 implements My {
@Override
public String turn() {
return "1";
}
}
以下单元测试失败,bean 列表为空。
@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 出了什么问题?
谢谢
【问题讨论】: