Search code examples
jbossjava-ee-6ejb-3.1

Connect to a running JBoss AS7 instance for test purposes


I already have a integration-test phase, when I ran the selenium tests. I also want to run some unit tests in this phase, because the app is too much complex and have a lot of dependencies between his modules (a hell), so, after a week fighting against OpenEJB and Arquillian, I believe that this would be easier.

The thing is: how do I made it work?

I have the instance already running, if I instantiate an InitialContext and try to lookup some bean, I got an exception telling me that I have not set the java.naming.initial.factory, and I don't know what to put in there.

I'm also complaining about the annotated beans.

Suppose a Bean like this:

@Stateless
public class ABeanImpl implements ABean {
  @EJB
  private BBean;
}

Will the container automatically get right the BBean?

Thanks in advance


Solution

  • How to connect to JBoss 7.1 remote JNDI:

    Here is the code snippet that I use for JBoss 7.1:

    Properties props = new Properties();
    String JBOSS_CONTEXT = "org.jboss.naming.remote.client.InitialContextFactory";
    props.put("jboss.naming.client.ejb.context", true);
    props.put(Context.INITIAL_CONTEXT_FACTORY, JBOSS_CONTEXT);
    props.put(Context.PROVIDER_URL, "remote://localhost:4447");
    props.put(Context.SECURITY_PRINCIPAL, "jboss");
    props.put(Context.SECURITY_CREDENTIALS, "jboss123");
    InitialContext ctx = new InitialContext(props);
    

    Resolution of ambiguous ejb references:

    According to JBoss EJB 3 reference, if at any level of your EJB environment (EJB/EAR/Server) are duplicates in used interfaces, exception will be thrown during resolution of injected beans.

    Based on above, if you have got a reference to EJB bean which interface:

    • has two implementations in your EJB module (JAR/WAR) - exception will be thrown
    • has two implementations in your application (other EJB JAR's in same EAR) - exception will be thrown
    • has two implementations, one in module with bean ABeanImpl, second somewhere else - implemetation from current module is used.