2014-10-29 97 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