我正在尝试编写使用JUnit,Struts2,Spring,JPA / Hibernate的集成测试。
该应用程序在WebSphere中运行良好。 Integration测试在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中设置。
我去了属性文件,用我在执行Websphere的dumpNamespaces.bat时获得的完整jndi url替换了本地jndi url。这就像
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
问题:
@BeforeClass
方法或静态初始化程序中覆盖jndi-name设置?因为,上面的JNDI URL,提供程序URL丢失。 答案 0 :(得分:1)
JNDI上下文通常由您的应用服务器(Websphere)提供。当您从Websphere外部运行测试时,您将无法访问本地JNDI。我看到3个解决方案......
确保您已连接到远程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,但不应该序列化数据源。它无法远程传输。
设置本地JNDI
Spring提供了一个SimpleNamingContextBuilder class,可以在@BeforeClass方法中用于设置本地JNDI并用手工制作的数据源填充
不要加载jpaContext.xml文件
创建另一个将提供dsSQLServer bean但不访问JNDI的bean。 SimpleDriverDataSource可能是完美的。
我首选的解决方案是#3。