2013-05-15 44 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