我有以下四个类:DataConsumer、DataProducer、SomeQualifier、一个 META-INF/beans.xml 和一个测试。类文件编码如下:
public class DataConsumer {
private boolean loaded = false;
@Inject
@SomeQualifier
private String someString;
public void afterBeanDiscovery(
@Observes final AfterBeanDiscovery afterBeanDiscovery,
final BeanManager manager) {
loaded = true;
}
public boolean getLoaded() {
return loaded;
}
public String sayHello() {
return someString;
}
}
public class DataProducer {
@Produces
@SomeQualifier
private final String sample = "sample";
}
public @interface SomeQualifier {
}
单元测试看起来像这样。
public class WeldTest {
@Test
public void testHelloWorld() {
final WeldContainer weld = new Weld().initialize();
final DataConsumer consumer = weld.instance()
.select(DataConsumer.class).get();
Assert.assertEquals("sample", consumer.sayHello());
Assert.assertTrue(consumer.getLoaded());
}
}
但是,它使用 getLoaded() 在 assertTrue 上失败,似乎 @Observes 没有被解雇。