我已经定义了一个包含内存中HSQLDB的Ant目标:
<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");
但只有一个空的数据库。是否可以将HSQLDB与JUnit测试用例中的Ant目标的insert语句一起使用?
junit测试中的例外情况如下。该表存在,因为它是在ant任务中创建的:
[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)
答案 0 :(得分:0)
这是Ant中的连接URL:
<property name="db.connection.url" value="jdbc:hsqldb:mem:adb"/>
您必须使用相同的URL和相同的用户名和密码进行连接:
connection = DriverManager.getConnection("jdbc:hsqldb:mem:adb", "a", "a");
更新:使用内存数据库时,Ant目标和JUnit测试必须在同一个JVM中运行。如果您在单独的JVM中运行这两个,请在测试之前启动HSQLDB服务器并从每个测试连接到此服务器。服务器连接URL是不同的,并在HSQLDB指南中进行了描述。