I'm using Deltaspike Test-Control for testing with JUnit within CDI container,
which works pretty nice for "scoped" beans. However I'm getting troubles with
dependent beans. Hier is an example:
public class DependentBean {
private String val;
// getter/setter
}
@RunWith(CdiTestRunner.class)
public class DependentBeanTest {
@Inject
private DependentBean bean;
@Before
public void before() {
System.out.println("before() bean: " +
System.identityHashCode(bean));
bean.setVal("nonnull");
}
@After
public void after() {
System.out.println("after() bean: " +
System.identityHashCode(bean));
}
@Test
public void test() {
System.out.println("test() bean: " +
System.identityHashCode(bean));
Assert.assertNotNull(bean.getVal());
}
}
Output:
before() bean: 794894657
test() bean: 479460026
after() bean: 479460026
The test fails and the output say, that in method before() the reference to the
bean is not the same as in methods test() and after(). Thus, the bean is being
injected twice.
Is it a bug or such behavior is expected?