0

I have this scenario:

  1. Enterprise Application. Runs ok. --> contains MyFacade
  2. Client application. Runs ok.
  3. Ejb module. Runs ok, but can't test --> contains MyClass.java
  4. DomainLibrary. --> contains entities and remote interfaces.

I want to run test file of point 3. (ejb module). Ok, first of all I put the Enterprise Application running. On second place I run the test file of the point 3. The problem is that the remote interface ejb contained on Enterprise Application can't be found.

Error 1: It end up with an endless loop of the following output

WARNING: AS-CDI-005 Okt 22, 2013 4:49:23 PM org.glassfish.weld.BeanDeploymentArchiveImpl handleEntry

Error 1: Solved Running JUnit Tests on embedded glassfish 4 causing WARNING: AS-CDI-005

Error 2!!!:

javax.naming.NamingException: Lookup failed for [...] (MyFacade)

Ejb module: MyClass.java

@Singleton
public class MyClass implements MyClassLocal {

    @EJB(lookup = "java:global/EnterpriseApplication-ejb/MyFacade!com.mydomain.repository.MyFacadeRemote")
    private MyFacadeRemote myFacade;

    public MyClass() {
    }

    public void bussinesMethod(){
        System.out.println("Hello stackOverWorld! ");
    myFacade.findAll();
    }
}

Test method:

@Test
public void testBusinessMethod() throws Exception {
    System.out.println("businessMethod");

    Map<Object, Object> properties = new HashMap<Object, Object>(); 
    properties.put(EJBContainer.APP_NAME, "MyEjbModule");
    properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
    EJBContainer container = EJBContainer.createEJBContainer(properties);
    MyClassLocal instance = (MyClassLocal)container.getContext().lookup("java:global/MyEjbModule/classes/MyClass!com.mydomain.MyClassLocal");
    //EJBContainer container = javax.ejb.embeddable.EJBContainer.createEJBContainer();
    //MyClassLocal instance = (MyClassLocal)container.getContext().lookup("java:global/classes/MyClass");

    instance.businessMethod();
    container.close();
}
Community
  • 1
  • 1
xav56883728
  • 315
  • 1
  • 8
  • 20

1 Answers1

2

You may use the same approach we did for our EJB application. You may call some of the tests 'unit tests' but they are really integration tests. Running these tests includes starting the application server first, then running the JUnit test cases and finally stopping the server. These test cases act as the client, loading the ejb remote interface, make the appropriate calls, validate the return values, etc just like your client would.

One of the problems with this is that these tests can take a while to run, depending on how many you have. It also requires the server be up and running.

I know there must be better ways, but this has worked for our project for about 10 years.

mikemil
  • 1,213
  • 10
  • 21