Search code examples
jakarta-eesingletoncdijboss-arquillian

CDI: Why am I getting two instances of javax.ejb.Singleton in my unit test?


Got a class MyService annotated with @javax.ejb.Singleton.

@Singleton
public class MyService() { 
    ..
}

The unit test (Arquillian) below fails (1 and 2):

@Inject private MyService myService1;
@Inject private MyService myService2;

@Test
public void singletonScopedTest() {

    // 1
    assertEquals(myService1, myService2);

    // 2
    assertTrue(myService1== myService2);
}

The same test applied to @ApplicationScoped produced beans will pass.

Question

Why the test fails for @Singleton but pass for @ApplicationScoped ?


Solution

  • The objects that are @Injected for EJBs are proxies, not the object instances themselves.

    Your unit tests are comparing the proxy objects.