0

我正在尝试对 struts2 操作进行单元测试(命中一个真实的数据库),但我无法成功连接到数据库。

我是 Spring 新手,项目设置在多个 bean 和 DAO 类中使用 jdbctemplate。

到目前为止,这是我的尝试:

测试类:

    public class Test extends StrutsSpringTestCase {

    private static final String serverURL = "jdbc:db2://xxxx.xxx.com:----/xxxx";
    private static final String username = "xxxxxxx";
    private static final String password = "xxxxxxx";

    @Override
    public String[] getContextLocations() {

    SimpleNamingContextBuilder s = new SimpleNamingContextBuilder();

    DB2SimpleDataSource db=new DB2SimpleDataSource(); 

    try {
    DriverManager.registerDriver( new com.ibm.db2.jcc.DB2Driver() );
    } catch (SQLException e2) {
    // TODO Auto-generated catch block
    e2.printStackTrace();
    }

    try {
    Class.forName("com.ibm.db2.jcc.DB2DataSource");
    } catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }

    db.setServerName(serverURL);
    db.setPortNumber(----);
    db.setUser(username);
    db.setPassword(password);

    try {

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

    Context javaCtx = ctx.createSubcontext("jdbc");
    javaCtx.bind("xxx", db);
    ctx.bind("java:", javaCtx);

    } catch (IllegalStateException e) {
    e.printStackTrace();
    } catch (NamingException e) {
    e.printStackTrace();
    }

    String[] locations = new String[1];

    locations[0] = "xxxx.xml";

    return locations;
    }

    public void test() throws Exception {

    ActionProxy proxy = getActionProxy("/test");

    testAction rd = (testAction) proxy.getAction();

    System.out.println(rd.testMethod());

    }
    }

应用程序上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<bean name="jndi" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/XXX" />
</bean>

<bean name="dataSource"     class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/>
<property name="url" value="jdbc:db2://----.---.com:xxxx/xxxx"/>
<property name="username" value="xxxxxxxx"/>
<property name="password" value="xxxxxxxx"/>
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg>
    <ref bean="dataSource" />
</constructor-arg>

</bean>

<bean id="xxxxDao"
    class="xxxx">
    <property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>

(继续使用其他与 DAO 相关的 bean)。

许多类都依赖于 JdbcTemplate,如下所示,所以我想找到一个包含它的解决方案:

private JdbcTemplate jdbcTemplate;
private String schemaName = "xxxxxx";
private String procedureName = "xxxxxx";

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

public RDResponseBO browseRD(RDRequestBO rdReqBO) {
    SimpleJdbcCall storedProc = new SimpleJdbcCall(jdbcTemplate)
            .withSchemaName(schemaName)
            .withoutProcedureColumnMetaDataAccess().withProcedureName(
                    procedureName); .........

我的 applicationContext.xml 包含一个数据源和一个 jndi bean,这有点多余。我不知道是否可以在 JdbcTemplate 中将它们结合起来以使连接成功。

单独使用数据源会导致缺少元数据/jdbc 异常......虽然我想使用 jndiobjectfactorybean 类,但如果不通过服务器运行,bean 不会在 JUnit 中初始化。

我尝试在 jdbctemplate bean 中包含 jndi bean(在调用 simplenamingcontextbuilder 之后),这导致了匹配的构造函数异常。

想法/建议?

PS如果问题格式有点不对,我很抱歉,我也是stackoverflow的新手。

jndi bean 错误的堆栈跟踪:

            .springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in class path resource [beans.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
            at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:250)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1003)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:907)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
            at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
            at org.apache.struts2.StrutsSpringTestCase.setupBeforeInitDispatcher(StrutsSpringTestCase.java:39)
            at org.apache.struts2.StrutsTestCase.setUp(StrutsTestCase.java:187)
            at junit.framework.TestCase.runBare(TestCase.java:128)
            at junit.framework.TestResult$1.protect(TestResult.java:106)
            at junit.framework.TestResult.runProtected(TestResult.java:124)
            at junit.framework.TestResult.run(TestResult.java:109)
            at junit.framework.TestCase.run(TestCase.java:120)
            at junit.framework.TestSuite.runTest(TestSuite.java:230)
            at junit.framework.TestSuite.run(TestSuite.java:225)
            at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
            at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
            at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

使用 SimpleNamingContextBuilder 之前的 JNDI 错误:

            org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jndi' defined in class path resource [beans.xml]: Invocation of init method failed; nested exception is com.ibm.websphere.naming.CannotInstantiateObjectException: Exception occurred while the JNDI NamingManager was processing a javax.naming.Reference object. [Root exception is java.lang.NoClassDefFoundError: com.ibm.wsspi.runtime.service.WsServiceRegistry]
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
                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:293)
                at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
                at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
                at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:192)
                at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:567)
                at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
                at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
                at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
                at org.apache.struts2.StrutsSpringTestCase.setupBeforeInitDispatcher(StrutsSpringTestCase.java:39)
                at org.apache.struts2.StrutsTestCase.setUp(StrutsTestCase.java:187)
                at junit.framework.TestCase.runBare(TestCase.java:128)
                at junit.framework.TestResult$1.protect(TestResult.java:106)
                at junit.framework.TestResult.runProtected(TestResult.java:124)
                at junit.framework.TestResult.run(TestResult.java:109)
                at junit.framework.TestCase.run(TestCase.java:120)
                at junit.framework.TestSuite.runTest(TestSuite.java:230)
                at junit.framework.TestSuite.run(TestSuite.java:225)
                at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
                at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
            Caused by: com.ibm.websphere.naming.CannotInstantiateObjectException: Exception occurred while the JNDI NamingManager was processing a javax.naming.Reference object. [Root exception is java.lang.NoClassDefFoundError: com.ibm.wsspi.runtime.service.WsServiceRegistry]
                at com.ibm.ws.naming.util.Helpers.processSerializedObjectForLookupExt(Helpers.java:1033)
                at com.ibm.ws.naming.util.Helpers.processSerializedObjectForLookup(Helpers.java:730)
                at com.ibm.ws.naming.jndicos.CNContextImpl.processResolveResults(CNContextImpl.java:3691)
                at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1861)
                at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(CNContextImpl.java:1762)
                at com.ibm.ws.naming.jndicos.CNContextImpl.lookupExt(CNContextImpl.java:1513)
                at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNContextImpl.java:645)
                at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:166)
                at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:180)
                at javax.naming.InitialContext.lookup(InitialContext.java:455)
                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:1477)
                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
                ... 25 more
            Caused by: java.lang.NoClassDefFoundError: com.ibm.wsspi.runtime.service.WsServiceRegistry
                at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl$2.run(WSManagedConnectionFactoryImpl.java:835)
                at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
                at com.ibm.ws.rsadapter.spi.WSManagedConnectionFactoryImpl.<init>(WSManagedConnectionFactoryImpl.java:840)
                at java.lang.J9VMInternals.newInstanceImpl(Native Method)
                at java.lang.Class.newInstance(Class.java:1325)
                at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.createMCF(ConnectionFactoryBuilderImpl.java:1491)
                at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.processObjectInstance(ConnectionFactoryBuilderImpl.java:746)
                at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.processObjectInstance(ConnectionFactoryBuilderImpl.java:705)
                at com.ibm.ejs.j2c.ConnectionFactoryBuilderImpl.getObjectInstance(ConnectionFactoryBuilderImpl.java:664)
                at javax.naming.spi.NamingManager.getObjectInstanceByFactoryInReference(NamingManager.java:485)
                at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:350)
                at com.ibm.ws.naming.util.Helpers.processSerializedObjectForLookupExt(Helpers.java:927)
                ... 44 more
            Caused by: java.lang.ClassNotFoundException: com.ibm.wsspi.runtime.service.WsServiceRegistry
                at java.lang.ClassNotFoundException.<init>(ClassNotFoundException.java:77)
                at java.net.URLClassLoader.findClass(URLClassLoader.java:383)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:652)
                at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:346)
                at java.lang.ClassLoader.loadClass(ClassLoader.java:618)
                ... 56 more

            javax.naming.CommunicationException: A communication failure occurred while attempting to obtain an initial context with the provider URL: "iiop://xxxx.xxx.com:----".  Make sure that any bootstrap address information in the URL is correct and that the target name server is running.  A bootstrap address with no port specification defaults to port 2809.  Possible causes other than an incorrect bootstrap address or unavailable name server include the network environment and workstation network configuration. [Root exception is org.omg.CORBA.COMM_FAILURE: purge_calls:1988 Reason: CONN_ABORT (1), State: ABORT (5)  vmcid: IBM  minor code: 306 completed: Maybe]
                at com.ibm.ws.naming.util.WsnInitCtxFactory.mapInitialReferenceFailure(WsnInitCtxFactory.java:2269)
                at com.ibm.ws.naming.util.WsnInitCtxFactory.getWsnNameService(WsnInitCtxFactory.java:1457)
                at com.ibm.ws.naming.util.WsnInitCtxFactory.getRootContextFromServer(WsnInitCtxFactory.java:987)
                at com.ibm.ws.naming.util.WsnInitCtxFactory.getRootJndiContext(WsnInitCtxFactory.java:909)
                at com.ibm.ws.naming.util.WsnInitCtxFactory.getInitialContextInternal(WsnInitCtxFactory.java:581)
                at com.ibm.ws.naming.util.WsnInitCtx.getContext(WsnInitCtx.java:124)
                at com.ibm.ws.naming.util.WsnInitCtx.getContextIfNull(WsnInitCtx.java:799)
                at com.ibm.ws.naming.util.WsnInitCtx.createSubcontext(WsnInitCtx.java:370)
                at com.ibm.ws.naming.util.WsnInitCtx.createSubcontext(WsnInitCtx.java:385)
                at javax.naming.InitialContext.createSubcontext(InitialContext.java:523)
                at junit.Test.getContextLocations(Test.java:88)
                at org.apache.struts2.StrutsSpringTestCase.setupBeforeInitDispatcher(StrutsSpringTestCase.java:39)
                at org.apache.struts2.StrutsTestCase.setUp(StrutsTestCase.java:187)
                at junit.framework.TestCase.runBare(TestCase.java:132)
                at junit.framework.TestResult$1.protect(TestResult.java:110)
                at junit.framework.TestResult.runProtected(TestResult.java:128)
                at junit.framework.TestResult.run(TestResult.java:113)
                at junit.framework.TestCase.run(TestCase.java:124)
                at junit.framework.TestSuite.runTest(TestSuite.java:232)
                at junit.framework.TestSuite.run(TestSuite.java:227)
                at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
                at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:45)
                at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
            Caused by: org.omg.CORBA.COMM_FAILURE: purge_calls:1988 Reason: CONN_ABORT (1), State: ABORT (5)  vmcid: IBM  minor code: 306 completed: Maybe
                at com.ibm.rmi.iiop.Connection.purge_calls(Connection.java:1987)
                at com.ibm.rmi.iiop.Connection.doReaderWorkOnce(Connection.java:3134)
                at com.ibm.rmi.transport.ReaderThread.run(ReaderPoolImpl.java:138)
4

1 回答 1

0

用这个替换你的

 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource" />
 </bean>
于 2012-06-12T18:16:59.900 回答