By the way, the
avoiding circular dependenciespart happens when I try to extract the @Before and @After behavior into a JUnit @Rule.
That is, if I factor out the WeldSE bootstrap and injection code to another class (a JUnit interceptor), I get the WELD-000018 Executing producer field or method ... on incomplete declaring bean ... due to circular injection warning and a java.util.EmptyStackException
For the record, my setup code is basically similar to how Weld injection into EJBs is described here:
WeldContainer weldContainer = weld.initialize(); BeanManager bm = weldContainer.getBeanManager(); Class<?> cl = target.getClass(); AnnotatedType<Object> at = (AnnotatedType<Object>) bm.createAnnotatedType(cl); InjectionTarget<Object> it = bm.createInjectionTarget(at); CreationalContext<Object> cc = bm.createCreationalContext(null); it.inject(target, cc);
Where target is just this in the simple case (in a @Before method), and it's the actual Test instance when it's a JUnit @Rule
Beans which contains producer methods need to be instantiated before producer methods can be executed. That's what happens in your case - so, just separate producer method and test (put it in its own class somewhere on your test classpath). You can also define the scope of mockDependency, in case that DependendtScope is not what you want.