Question

We are using AbstractOpenEJBTestNG class and openEJB to load the classes. Many classes are loaded with the statement below:

ejbJar.addEnterpriseBean(new StatelessBean(MyService.class));

But I also see @EJB annotation at the top of the class. I was under understanding that, to load EJBs in openEJB container, we need to use the above statement. But I am not sure if we can use @EJB annotations as well. So we can either of them? Or any specific use cases for each method?

Thanks!

Was it helpful?

Solution

Not sure if I understand your concern correctly, but to perform integration tests with EJBs you need both things:
1. At first you need to make OpenEJB aware of any EJBs it can provide in the forthcoming test. That's what the ejbJar.addEnterpriseBean(new StatelessBean(MyService.class)); part is for.
2. Secondly, you specify all the EJB injections used by your test directly, just in the same manner, as you would do in your normal code. Remember that, if the EJB you are invoking in your test is dependent on any other EJBs, you need to specify all of them using the .addEnterpriseBean() method. Example:

public class SomeProcessingServiceIntegrationTest extends AbstractOpenEJBTestNG {

@EJB
SomeProcessingServiceLocal processingService;

@Module
public EjbModule beans() {

    EjbJar ejbJar = new EjbJar("processing");
    OpenejbJar openejbJar = new OpenejbJar();

    ejbJar.addEnterpriseBean(new StatelessBean(SomeProcessingService.class));
    ejbJar.addEnterpriseBean(new StatelessBean(DatabaseLookupService.class));
}
@Test
public void testProcessingService(){
     // Should return true for example
     assertTrue(processingService.process());
}
}

@Stateless
@Local(SomeProcessingServiceLocal.class)
public class SomeProcessingService {

@EJB
DatabaseLookupServiceLocal databaseLookupService;

    public boolean process(){
         return databaseLookupService.existsInDB();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top