Search code examples
javahibernatejunitjbossjndi

How registry JNDI datasource for JUnit for Jboss environment


Project use Hibernate and deployed on Jboss 5.1. Hibernate uses JNDI to get datasource.

I want create JUnit tests for DAO layer and for this I need create JNDI data source and transaction manager to test without running Jboss.

For this I wrote the code:

System.out.println(LocateRegistry.getRegistry());
Registry reg = LocateRegistry.createRegistry(1099);


Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
properties.put(Context.PROVIDER_URL, "localhost:1099");

InitialContext initialContextcontext = new InitialContext(properties);

When I try to lookup or bind the named object application is frizz. And after some time throw timeout exception. Bind subcontext throws the same exception

In Maven I include dependency:

  <groupId>org.jboss.client</groupId>
  <artifactId>jbossall-client</artifactId>
  <version>4.2.3.GA</version>

After research I have faced with this article http://something-about-tech.blogspot.com/2008/12/injecting-jndi-datasource-for-junit.html . But there the line like this Registry reg = LocateRegistry.createRegistry(1099); is missed.. I in confusion...

In related post (Registering MySQL DataSource with JNDI for Hibernate) I have interested about registry datasource but there was

Context.INITIAL_CONTEXT_FACTORY="com.sun.jndi.rmi.registry.RegistryContextFactory"

Please help.


Solution

  • org.jnp.interfaces.NamingContextFactory is JBoss specific protocol, you cannot use it to connect to RMI registry. Try this test

    public class Test1 implements Serializable, Remote {
    
        public static void main(String[] args) throws Exception {
            Registry reg = LocateRegistry.createRegistry(1099);
            Context ctx = new InitialContext();
            ctx.bind("rmi://localhost/xxx", new Test1());
            Test1 t1 = (Test1) ctx.lookup("rmi://localhost/xxx");
            System.out.println(t1);
        }
    }