2013-05-15 35 views
0

项目使用Hibernate并部署在Jboss 5.1上。 Hibernate使用JNDI来获取数据源。JUnit for Jboss环境的注册表JNDI数据源

我想为DAO层创建JUnit测试,为此我需要创建JNDI数据源和事务管理器来测试而无需运行Jboss。

为此,我写的代码:

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); 

当我尝试查找或绑定命名对象的应用程序是毛躁。并在一段时间后抛出超时异常。绑定子上下文抛出同样的异常

在我的Maven依赖包括:

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

我也碰到这样的文章http://something-about-tech.blogspot.com/2008/12/injecting-jndi-datasource-for-junit.html研究之后。但是,像这样Registry reg = LocateRegistry.createRegistry(1099);行错过..我在混乱......

在相关岗位(Registering MySQL DataSource with JNDI for Hibernate)我有兴趣有关注册表数据源,但有

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

请帮助。

回答

1

org.jnp.interfaces.NamingContextFactory是JBoss特定的协议,您不能使用它连接到RMI注册表。试试这个测试

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); 
    } 
} 
+0

对不起,不明白。但我该如何绑定?我试着把属性PROVIDER_URL =“rmi:// localhost:1099”,但得到异常“无法连接到服务器”。你能详细解释一下吗? – Tioma

+0

其实,如果你有一个DataSource为什么绑定?为什么不直接使用它? –

+0

谢谢,现在看起来没问题。我绑定数据源,因为休眠conf使用jndi来获取它。因此,为了测试目的,我需要绑定DataSource来测试与生产相同的东西。再次感谢你的帮助。 – Tioma