16

In my project I can successfully test database code. I'm using Spring, Hibernate, HSQLDB, JUnit and Maven.

The catch is that currently I have to launch HSQLDB manually prior to running the tests. What is the best way to automate the launching of HSQLDB with the technologies being used?

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
tom eustace
  • 1,241
  • 2
  • 13
  • 20
  • Currently I am working on an application where a in-memory Database is needed for automated JUnit tests. Following article answered a lot of my questions: [http://tshikatshikaaa.blogspot.de/2012/09/junit-testing-spring-service-and-dao.html](http://tshikatshikaaa.blogspot.de/2012/09/junit-testing-spring-service-and-dao.html) – Christian Müller Nov 30 '12 at 16:05

5 Answers5

14

I am assuming that with hsql you are referring to HSQLDB.

Configure your database url for JDBC drivers (for hibernate etc) to embedded memory based version of HSQLDB:

jdbc:hsqldb:mem:myunittests

Then a inprocess version of HSQLDB automatically starts that stores stuff to memory. No need to start any external servers.

Juha Syrjälä
  • 33,425
  • 31
  • 131
  • 183
  • Thanks, just getting started with HSQLDB. The tutorial I was following was configured for using a server. Changing to use in memory solves my problem. – tom eustace Jun 16 '10 at 11:52
4

I myself use in-memory database of hsql for testing my DAO. As a result, I need not be connected to any external db server or have any network connection.
Use following settings:

jdbc.driverClassName=org.hsqldb.jdbc.JDBCDriver

jdbc.url=jdbc:hsqldb:mem:DatabaseName

Also include the

<property name="hibernateProperties">
   <props>
     <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
     <prop key="default_schema">test</prop>
     <prop key="hibernate.show_sql">true</prop>
     <prop key="hibernate.format_sql">false</prop>
     <prop key="hibernate.hbm2ddl.auto">create</prop>
   </props>
</property>

This will allow you to use the in-memory database and will automatically create the database tables from hibernate objects before executing tests.

Hope this will help you.

Note:

The "default_schema" property is used when your DBA creates multiple schemas within a single database. I've seen this with postgres where everyone uses one database URL but under that there are separate schemas for each application.

By using the default schema property it allows you to keep the schema names off your entities. This is particularly useful if you're running tests against HSqlDB which does not support schemas and you deploy against a DB that is using schemas. Having a null value just means it defaults back to the DB default schema.

nishant
  • 91
  • 6
1

You can also run an Ant task <startdb>:

https://forums.hibernate.org/viewtopic.php?f=6&t=984383&start=0

duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Use it in-process or in memory and it will get started from JDBC when establishing a connection.

Akhorus
  • 2,233
  • 20
  • 24
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
-3

With JUnit, you can create a method that is executed before your tests using the following annotation: @Before

The link to the JUnit docs about it is here: JUnit FAQ - Test Fixtures

Chris J
  • 9,164
  • 7
  • 40
  • 39