【问题标题】:Could not get JDBC connection in JUnit无法在 JUnit 中获得 JDBC 连接
【发布时间】:2015-05-19 11:28:36
【问题描述】:

我看到了与该问题相关的其他帖子,但似乎无法理解它和/或应用我的代码来解决该问题。因此,您的帮助将非常有帮助。

这是错误堆栈跟踪:

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver'
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:627)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:692)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:724)
at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:734)
at org.springframework.jdbc.core.JdbcTemplate.queryForRowSet(JdbcTemplate.java:899)
at com.cinfin.edocs.services.dao.FormMatrixDaoImpl.validateNonProductionStatusInMatrix(FormMatrixDaoImpl.java:857)
at com.cinfin.edocs.services.dao.FormMatrixDaoImplTest.testValidateNonProductionStatusInMatrix(FormMatrixDaoImplTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:81)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:216)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:82)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:60)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:67)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:162)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.sql.SQLException: Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver'
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2001)
at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1897)
at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1413)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
... 36 more
Caused by: java.lang.ClassNotFoundException: com.ibm.db2.jcc.DB2Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.apache.commons.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:1992)
... 40 more

我的 FormMatrixDaoImpl 类:

import javax.sql.DataSource;

@Repository
public class FormMatrixDaoImpl extends NamedParameterJdbcDaoSupport implements FormMatrixDao {

@Autowired  
public FormMatrixDaoImpl(@Qualifier("vitDataSource") DataSource dataSource, @Value("#{edocsQueryMap}") HashMap<String, String> edocsQueryMap) {
    super();

        @Override
    public SqlRowSet validateNonProductionStatusInMatrix(String projectId) throws SQLException {
        logger.info("validateNonProductionStatusInMatrix. projectId: "+projectId);

        SqlRowSet ret = getJdbcTemplate().queryForRowSet(qValidateNonProductionStatusInMatrix, new Object[] {projectId});

        return ret;
    }
}

我的 FormMatrixDaoImplTest 类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:datasources.xml","classpath:edocservices.xml","classpath:edocservicesclients.xml", "/WEB-INF/edocservices-servlet.xml"})
@ActiveProfiles("local")

public class FormMatrixDaoImplTest {

@Autowired
private FormMatrixDaoImpl formMatrixDaoImpl;
public FormMatrixDaoImpl getFormMatrixDaoImpl() {
    return formMatrixDaoImpl;
}
public void setFormMatrixDaoImpl(FormMatrixDaoImpl formMatrixDaoImpl) {
    this.formMatrixDaoImpl = formMatrixDaoImpl;
}

@Test
public final void testValidateNonProductionStatusInMatrix() throws SQLException {
    SqlRowSet test = formMatrixDaoImpl.validateNonProductionStatusInMatrix("0b003e8880072976");
    assertEquals(test,null);
    }

我的 datasources.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:util="http://www.springframework.org/schema/util" 
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd"> 

<beans profile="production,user,quality,development">         
    <bean id="vitDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <description>JNDI VITEDOC DB2 datasource</description>
        <property name="jndiName" value="${database.vitedoc.jndi}"/>        
    </bean>
    <bean id="docDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true">
        <description>JNDI DOCEDOC DB2 datasource</description>
        <property name="jndiName" value="${database.docedoc.jndi}"/>        
    </bean>

    <beans profile="local">     
    <bean id="vitDataSource" class="org.apache.commons.dbcp2.BasicDataSource" lazy-init="true" destroy-method="close">
        <description>Standalone VITEDOC datasource</description>
        <property name="driverClassName" value="${database.vitedoc.driver.classname}"/>
        <property name="url" value="${database.vitedoc.url}" />
        <property name="username" value="${database.vitedoc.username}"/>
        <property name="password" value="${database.vitedoc.password}"/>
    </bean>
    <bean id="docDataSource" class="org.apache.commons.dbcp2.BasicDataSource" lazy-init="true" destroy-method="close">
        <description>Standalone DOCEDOC datasource</description>
        <property name="driverClassName" value="${database.docedoc.driver.classname}"/>
        <property name="url" value="${database.docedoc.url}" />
        <property name="username" value="${database.docedoc.username}"/>
        <property name="password" value="${database.docedoc.password}"/>
    </bean>
</beans>

我的 edocservices.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:ws="http://www.springframework.org/schema/web-services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:c="http://www.springframework.org/schema/c"    
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" 
xmlns:jms="http://www.springframework.org/schema/jms"   
xsi:schemaLocation="http://www.springframework.org/schema/web-services http://www.springframework.org/schema/web-services/web-services.xsd
                    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
                    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd                       
                    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd                      
                    http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.1.xsd">

上面的xml有marshallers、unmarshallers、endpoints & wsdls。

这是我的 edocservicesclient.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:util="http://www.springframework.org/schema/util"     
xmlns:context="http://www.springframework.org/schema/context"   
xmlns:oxm="http://www.springframework.org/schema/oxm"       
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd       
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd">

<context:property-placeholder location="classpath:edocservices.properties"/>
<context:component-scan base-package="com.cinfin.edocs.services"/>

上面的xml还有一些其他的marshaller、unmarshallers和saajmessagefactory。

这是我的 edocservices-servlet.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:oxm="http://www.springframework.org/schema/oxm"
xmlns:mvc="http://www.springframework.org/schema/mvc"  xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:task="http://www.springframework.org/schema/task"               
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd  
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
    http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd      
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
    http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">

<context:property-placeholder location="classpath:edocservices.properties"/>
<context:component-scan base-package="com.cinfin.edocs.services"/>  
<context:annotation-config/>

当我将 FormMatrixDaoImplTest 类作为 JUnit 测试运行时,我收到无法获取 jdbc 连接错误。此错误仅与 JUnit 测试相关,程序在本地运行时不会出现。所以我有一种感觉,我可能会遗漏 JUnit 设置或类似的东西。

任何指出代码中的错误/错误和/或指向其他方式来测试运行“选择”命令以从数据库中检索数据的帮助将不胜感激。

提前谢谢你。

【问题讨论】:

  • 所以,顺便说一句,看起来包含您的驱动程序类的 jar 在测试期间不可用。你在使用 Maven 吗?摇篮?确保依赖的范围包括“test”。
  • 听起来您可能在 WebSphere 上使用 Spring。您日志中的核心错误是Cannot load JDBC driver class 'com.ibm.db2.jcc.DB2Driver。建议:1) 跟踪您的 JNDI 定义(例如,vitDataSource)在哪里定义了它们的驱动程序类名(例如。database.vitedoc.driver.classname)2) 确保此 DB2Driver 在您的 WebSphere 管理控制台中作为 JDBC 资源可用,和/或 3 ) 确保在 pom.xml 中配置了 DB2 驱动程序 .jar(如果您使用的是 Maven)。
  • 感谢您的回复。我们没有使用 Maven 或 Hibernate,只是使用了 Spring。 @BillJames 如何使包含驱动程序类的 jar 在测试期间可用?
  • @paulsm4:我检查了属性文件,类名定义为:database.vitedoc.driver.classname=com.ibm.db2.jcc.DB2Driver
  • 请告诉我们您是如何进行此测试的?

标签: java xml spring jdbc junit


【解决方案1】:

错误无法加载 JDBC 驱动程序类“com.ibm.db2.jcc.DB2Driver”表明包含类的 jar 在运行时类路径中不可用。确保包含类的 jar 可用。

参考:How to Set Runtime classpath with eclipse

【讨论】:

  • 这完全有效。我有 db2jcc.jar 是项目类路径,但是,不在 JUnit 类路径中(我不知道我们需要两种不同的配置)。我接受这个答案,因为这更具体到 JUnit 运行配置。问题已解决 - 是时候运行一些 JUnit 测试了。谢谢大家。
  • 通常,从 Eclipse 运行时,构建类路径是运行时 class-path 的一部分,因此您无需显式更改或配置它。但有时当运气好时你需要:)
【解决方案2】:

1)从我上面的笔记:

听起来您可能正在 WebSphere 上使用 Spring。核心 您的日志中的错误是Cannot load JDBC driver class com.ibm.db2.jcc.DB2Driver

建议:

  1. 跟踪您的 JNDI 定义(例如,vitDataSource)定义其驱动程序的位置 类名(如database.vitedoc.driver.classname

2) 确保这一点 DB2Driver 在您的 WebSphere Admin 中可用作 JDBC 资源 安慰 ...和/或...

3) 确保 DB2 驱动程序 .jar 在您的 pom.xml(如果您使用的是 Maven)。

2) 您已确认:

"...类名定义为:database.vitedoc.driver.classname=com.ibm.db2.jcc.DB2Driver"

3) 所以现在你需要做的就是:

a) 确保您有一个有效的 db2jcc.jar:

http://www-01.ibm.com/support/docview.wss?uid=swg21363866

b) 确保 .jar 在类路径中

如果您正在运行 WebSphere,我的首选是在 WebSphere 管理控制台中执行此操作:

http://www-01.ibm.com/support/knowledgecenter/SSAW57_8.5.5/com.ibm.websphere.nd.doc/ae/tdat_ccrtpds.html?cp=SSAW57_8.5.5%2F1-3-0-23-3-0-7-2

如果这是一个简单的 Web 应用程序,您可以将其添加到 Eclipse 项目的 \lib 文件夹中:

http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.wst.webtools.doc.user%2Ftopics%2Fccwebprj.html

【讨论】:

    【解决方案3】:

    以下是向 Eclipse 添加第三方库的方法。

    1. 打开 Eclipse 转到包资源管理器。
    2. 右键单击您的项目并选择项目属性。
    3. 单击左侧栏中的 java Build path 弹出。
    4. 选择库选项卡。
    5. 选择“添加外部 Jar 文件”并使用浏览器导航到您的 JAR 文件并选择它们“打开”,然后选择“确定”。

    【讨论】:

      猜你喜欢
      • 2023-03-09
      • 2020-07-05
      • 2016-10-28
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2018-06-04
      • 1970-01-01
      相关资源
      最近更新 更多