Reputation: 1023
I am writing unit tests for a spring mvc 3.1 application that is not using hibernate.
I want to create tables in an hsql DB via the sql scripts I created to generate the tables in an oracle DB.
I've looked around for ways to do this but couldn't find anything helpful.
Ideally, I'd like to create the tables during setup, execute the tests, and then delete the tables.
Any ideas?
Upvotes: 3
Views: 2535
Reputation: 1023
Thank you @matsev for this~! This helped. I ended up using a slightly different configuration since I wanted to use HSQL to be my test db for an Oracle production DB.
What I ended up doing was this:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:test;sql.syntax_ora=true"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
<jdbc:script location="file:Artifacts/sql/install.sql"/>
<jdbc:script location="file:Artifacts/sql/patchset/1.0.0.02.create_survey_tables.sql"/>
<jdbc:script location="file:Artifacts/sql/patchset/1.00.01.update.sql"/>
<jdbc:script location="file:Artifacts/sql/patchset/1.00.03.insert_surveyQA2.sql"/>
<jdbc:script location="file:Artifacts/sql/patchset/1.00.05.insert_surveyQA4.sql"/>
</jdbc:initialize-database>
Upvotes: 3
Reputation: 33749
Have you looked at the Embedded Database Support in the Spring docs?
Create your database like this:
<jdbc:embedded-database id="dataSource">
<jdbc:script location="classpath:schema.sql"/>
<jdbc:script location="classpath:test-data.sql"/>
</jdbc:embedded-database>
(Optionally you can specify a type, but it defaults to HSQL). Add the appropriate HSQL dependencies to your classpath.
The embedded database will be created when the application context is created. By making your test @Transactional, there will be a rollback after each test has been executed, causing the tables to revert to its original, known state before the next test begins. Consequently, there is no need for deleting and recreating the tables per test.
Upvotes: 3