Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

testing - Starting an H2 Database Server from Maven?

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 to start an H2 database server for the tests and stop it when it's done?

I imagine this working similar to how I can run tomcat via a Maven command (mvn tomcat:run).

Sorry if this question is nonsensical, I'm still wrapping my head around new concepts.

question from:https://stackoverflow.com/questions/2205126/starting-an-h2-database-server-from-maven

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:h2db"/>
    <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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...