1

I have the following four classes: DataConsumer, DataProducer, SomeQualifier, a META-INF/beans.xml and a test. The class files are coded as follows:

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 {

}

The unit test looks like this.

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());
}
}

However, it is failing on the assertTrue with getLoaded() it appears that the @Observes does not get fired.

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

2 Answers2

0

Take a look at arquillian: www.arquillian.org. It'll take care of all of this for you.

LightGuard
  • 5,298
  • 19
  • 19
0

I found a similar question that had answered my question

CDI - Observing Container Events

Although I am unable to use DataConsumer as both an Extension and a CDI managed bean. So it needs a third class just to be the Extension. However, because Extension have no access to managed beans since they are not created yet, I conclude that is no possible solution to use an @Observes AfterBeanDiscovery to modify the bean data. Even the BeanManager that gets passed in cannot find any of the beans.

Community
  • 1
  • 1
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265