I have a class that looks like this:
@Singleton public class MySingletonImpl implements MySingleton{ @Override public void init(){ ... } @Override public void test(){ ... } }
It is called from the ApplicationComposer testng test, which looks like this:
@Listeners(ApplicationComposerListener.class) public class MyTest{ @EJB MySingleton mySingleton; @Module @Classes(cdi=true, value={MySingletonImpl.class}) public EjbModule ejbModule() throws Exception{ return new EjbModule(new EjbJar()); } @BeforeClass public void setup(){ mySingleton.init(); } @Test public void test(){ mySigleton.test(); } }
The problem that I observe when starting the test is that the object id of the MySingletonImpl class MySingletonImpl called by the test() method does not match the instance that the init() method is called on.
The behavior seems strange.
Firstly, how can my problem be fixed? I want to initialize and then call methods on the same object, not different instances of the same class.
Secondly, why the container did not create some @Singleton s?
source share