春季-如何在单元测试中使用内存中HSQLDB序列?
作者:互联网
我的应用程序使用序列,并且我尝试使用具有类似设置的HSQLDB内存数据库建立junit测试环境.这就是我配置序列及其创建的方式:
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true"
destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:test"/>
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="logSeqIncrementer" class="org.springframework.jdbc.support.incrementer.HsqlSequenceMaxValueIncrementer">
<property name="dataSource" ref="dataSource" />
<property name="incrementerName" value="public.agent_logs_seq" />
</bean>
<bean id="offerSeqIncrementer" class="org.springframework.jdbc.support.incrementer.HsqlSequenceMaxValueIncrementer">
<property name="dataSource" ref="dataSource" />
<property name="incrementerName" value="public.offers_seq" />
</bean>
<jdbc:embedded-database
id="test"
type="HSQL">
<jdbc:script
location="classpath:/create-ddl.sql" />
</jdbc:embedded-database>
</beans>
以及create-ddl.sql的内容:
CREATE SEQUENCE public.agent_logs_seq;
结果是
java.sql.SQLException: Sequence already exists in statement
但是如果我把它注释掉,我会
java.lang.AssertionError: Unexpected exception:
org.springframework.dao.DataAccessResourceFailureException:
Could not obtain sequence value; nested exception is
java.sql.SQLException: Sequence not found:
AGENT_LOGS_SEQ in statement [call next value for public.agent_logs_seq]
我尝试使用序列的方式:
DataFieldMaxValueIncrementer incrementer =
(DataFieldMaxValueIncrementer) context.getBean("logSeqIncrementer");
Integer logId = Integer.valueOf(incrementer.nextIntValue());
编辑:更改了上面的详细信息,以便使用正确的序列类.但是,它没有解决问题.
解决方法:
我有一个类似的问题,提出了here.
提供了各种解决方案. H2数据库似乎很有趣.最后,我手动删除并在脚本中创建序列.
标签:junit,hsqldb,spring 来源: https://codeday.me/bug/20191101/1979996.html