2014-10-29 68 views
2

我正在嘗試使用JUnit,Struts2,Spring,JPA/Hibernate編寫集成測試。jndi-name屬性覆蓋 - JUnit - Spring - Struts集成測試

該應用程序在WebSphere中正常工作。集成測試在WebSphere之外運行。

struts2-junit-plugin和spring-struts插件都被使用。我的測試課程延伸StrutsSpringTestCase

這是在測試方法的示例代碼:

ActionProxy proxy = getActionProxy("custSearch"); 
assertNotNull(proxy); 

它加載struts.xml中,applicationContext.xml中和jpaContext.xml。

在jpaContext.xml中,我有以下條目。

<bean id="dsSQLServer" class="org.springframework.jndi.JndiObjectFactoryBean" p:jndi-name="${sql.jndi.name}"/> 

sql.jndi.name來自屬性文件並在WebSphere中設置。

我去了屬性文件,並用完整的jndi網址替換了本地的jndi網址,這是我從執行websphere的dumpNamespaces.bat中得到的。這就像

MycomnameNode01Cell/nodes/C1A-mYNode/servers/mYSrv/jdbc/sql_src 

但是,當我從eclipse運行JUnit測試用例時,我得到以下異常。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceSQLServer' defined in URL [file:/C:/workspace/context/jpaContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:591) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:469) 
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:148) 
    at org.apache.struts2.StrutsSpringTestCase.setupBeforeInitDispatcher(StrutsSpringTestCase.java:38) 
    at org.apache.struts2.StrutsTestCase.setUp(StrutsTestCase.java:187) 
    at junit.framework.TestCase.runBare(TestCase.java:139) 
    at junit.framework.TestResult$1.protect(TestResult.java:122) 
    at junit.framework.TestResult.runProtected(TestResult.java:142) 
    at junit.framework.TestResult.run(TestResult.java:125) 
    at junit.framework.TestCase.run(TestCase.java:129) 
    at junit.framework.TestSuite.runTest(TestSuite.java:255) 
    at junit.framework.TestSuite.run(TestSuite.java:250) 
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:131) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
Caused by: javax.naming.NoInitialContextException: Failed to create InitialContext using factory specified in hashtable 
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:245) 
    at javax.naming.InitialContext.initializeDefaultInitCtx(InitialContext.java:318) 
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:348) 
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:428) 
    at javax.naming.InitialContext.lookup(InitialContext.java:436) 
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) 
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) 
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) 
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) 
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) 
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) 
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201) 
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) 
    ... 25 more 

問題:

  1. 應的JNDI URL會有所不同?是否可以指定完整的URL,如iiop:localhost:/?
  2. 是否可以覆蓋@BeforeClass方法中的jndi-name設置或使用靜態初始化程序?因爲,上面的JNDI URL提供者URL丟失。

回答

1

JNDI上下文通常由您的應用服務器(Websphere)提供。當您從Websphere外部運行測試時,您將無法訪問本地JNDI。我看到3個解決方案...

  1. 確保您連接到遠程JNDI

    你應該在@BeforeClass方法初始化您的訪問屬性:

    Hashtable env = new Hashtable(); 
    env.put(Context.PROVIDER_URL, "iiop://server:2809"); 
    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory"); 
    Context ctx = new InitialContext(env); 
    

    這樣你將能夠訪問遠程JNDI,但數據源不應被序列化。它不能被遠程轉移。

  2. 設置本地JNDI

    春天提供可在@BeforeClass方法被用來建立一個本地JNDI,並用手工填寫的數據源它SimpleNamingContextBuilder class

  3. 不要加載jpaContext.xml文件

    創建另一個將提供dsSQLServer bean但不訪問JNDI的文件。A SimpleDriverDataSource可能是完美的。

我的首選解決方案是#3。

+0

我喜歡你的方法。讓我試試最後一個。 – 2014-10-30 14:26:13