我正在尝试单元测试(JUnit)我创建的DAO.我使用Spring作为我的框架,我的DAO(JdbcPackageDAO)扩展了SimpleJdbcDaoSupport.测试类(JdbcPackageDAOTest)扩展了AbstractTransactionalDataSourceSpringContextTests.我已经覆盖了configLocations,如下所示:
protected String[] getConfigLocations(){ return new String[] {"classpath:company/dc/test-context.xml"}; }
我的test-context.xml文件定义如下:
company/data/dao/jdbc.properties
我使用HSQL作为我的后端,它以独立模式运行.我选择的IDE是eclipse.当我作为JUnit测试运行类时,这是我的错误(下面).我不知道为什么会发生这种情况.根据Eclipse,hsql.jar在我的构建路径上.
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: No suitable driver found for jdbc:hsqldb:hsql://localhost at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:219) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:377) at org.springframework.test.AbstractTransactionalSpringContextTests.startNewTransaction(AbstractTransactionalSpringContextTests.java:387) at org.springframework.test.AbstractTransactionalSpringContextTests.onSetUp(AbstractTransactionalSpringContextTests.java:217) at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:101) at junit.framework.TestCase.runBare(TestCase.java:128) at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76) 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: java.sql.SQLException: No suitable driver found for jdbc:hsqldb:hsql://localhost at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:291) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:277) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:259) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:241) at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:182) ... 18 more
Ivan Koblik.. 29
为了让HSQLDB自身注册,您需要访问其jdbcDriver类.您可以使用与此示例相同的方式执行此操作.
Class.forName("org.hsqldb.jdbcDriver");
它触发jdbcDriver类的静态初始化,即:
static { try { DriverManager.registerDriver(new jdbcDriver()); } catch (Exception e) {} }
我看到了,但我有类似的问题,解决方法如上所示.所以我想为什么不与我的同伴溢出者分享;) (7认同)
duffymo.. 10
"没有合适的驱动程序"通常意味着连接URL的语法不正确.
为了让HSQLDB自身注册,您需要访问其jdbcDriver类.您可以使用与此示例相同的方式执行此操作.
Class.forName("org.hsqldb.jdbcDriver");
它触发jdbcDriver类的静态初始化,即:
static { try { DriverManager.registerDriver(new jdbcDriver()); } catch (Exception e) {} }
"没有合适的驱动程序"通常意味着连接URL的语法不正确.