Starting an H2 Database Server from Maven?

后端 未结 9 1681
不思量自难忘°
不思量自难忘° 2021-01-30 07:09

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

相关标签:
9条回答
  • 2021-01-30 07:45

    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

    0 讨论(0)
  • 2021-01-30 07:45

    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.

    0 讨论(0)
  • 2021-01-30 07:47

    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.

    0 讨论(0)
提交回复
热议问题