0
我已經定義含有inmemory HSQLDB Ant目標:HSQLDB inmemory ant目標JUnit測試
<target name="create.tables" depends="-prepare.test">
<property name="db.connection.url" value="jdbc:hsqldb:mem:adb"/>
<property name="db.driver" value="org.hsqldb.jdbcDriver"/>
<property name="db.username" value="a"/>
<property name="db.password" value="a"/>
<echo>Creating tables using: ${db.driver} ${db.connection.url} ${product.mysql-connector.jar}</echo>
<sql driver="${db.driver}"
url="${db.connection.url}"
userid="${db.username}"
password="${db.password}"
onerror="stop"
src="/create-schema.sql">
<classpath refid="test.classpath" />
</sql>
</target>
的prepare.test僅包含當我打印SQL插入一切都是基本的事情,如文件集等 好。但是,如何從JUnit測試用例訪問內存中的HSQLDB呢?
我想:
Class.forName("org.hsqldb.jdbcDriver");
connection = DriverManager.getConnection("jdbc:hsqldb:mem:adb",
"a", "a");
,但只有一個空數據庫。是否可以在JUnit測試用例中使用HSQLDB和來自Ant目標的插入語句?
junit測試的例外情況如下。該表存在,因爲它是在螞蟻的任務發佈:
[junit] java.lang.Exception: user lacks privilege or object not found: MYTABLE
[junit] at com.inmemorytests.InMemoryTest.testSomething(InMemoryTest.java:116)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[junit] at java.lang.reflect.Method.invoke(Method.java:601)
[junit] at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
[junit] at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
[junit] at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
[junit] at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
[junit] at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
[junit] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
[junit] at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
[junit] at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
[junit] at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
[junit] at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
[junit] at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
[junit] at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
[junit] at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
[junit] at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:518)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1052)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:906)
謝謝你的回答,我現在用正確的憑據試了一下。但它仍然無法正常工作。在jUnit測試中,我得到了一個例外 – Julz 2013-05-07 06:21:44
更新了答案。 – fredt 2013-05-07 08:12:45
再一次,謝謝。現在它可以工作。我配置它像fredt說,並使用config.properties(我們的標準配置)使用** jdbc:hsqldb:mem:adb **作爲連接URL – Julz 2013-05-27 06:53:54