Suppose I want to create and use an H2 database for my integration tests.
Maven has a command to run tests: mvn test
.
Is there a way to tell maven t
Since H2 doesn't provide Maven plugin you should start it using maven-antrun-plugin. Write code for start and stop h2 engine in ant task and call it when your integration test starts and stop.
See details on http://docs.codehaus.org/display/MAVENUSER/Maven+and+Integration+Testing
I was able to get it to work without using an external server just by adding the dependency to H2 via Maven and then using this bean:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:file:h2\db"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
Then again, this required that I use a file-based DB instead of in-memory. But it does the trick.
In my project, for unit testing, I asked Spring to handle this database creation and initialization. As stated in the H2 documentation, you can create a bean for that:
<bean id = "org.h2.tools.Server"
class="org.h2.tools.Server"
factory-method="createTcpServer"
init-method="start"
destroy-method="stop">
<constructor-arg value="-tcp,-tcpAllowOthers,true,-tcpPort,8043" />
</bean>
You simply need to start the Spring context with this configuration when you start your unit tests.