Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring configuration for embedded H2 database for tests

What does your Spring configuration for integration tests look like using an embedded h2 datasource and, optionally, JUnit?

My first try with a SingleConnectionDataSource basically worked, but failed on more complicated tests where you need several connections at the same time or suspended transactions. I think h2 in tcp based server mode might work as well, but this is probably not the fastest communication mode for a temporary embedded database in memory.

What are the possibilities and their advantages / disadvantages? Also, how do you create the tables / populate the database?


Update: Let's specify some concrete requirements that are important for such tests.

  • The database should be temporary and in memory
  • The connection should probably not use tcp, for speed requirements
  • It would be nice if I could use a database tool to inspect the content of the database during debugging
  • We have to define a datasource since we can't use the application servers datasource in unit tests
like image 552
Hans-Peter Störr Avatar asked Jan 06 '10 10:01

Hans-Peter Störr


People also ask

How do I enable H2 database console in spring boot?

H2 Console: By default, the console view of the H2 database is disabled. Before accessing the H2 database, we must enable it by using the following property. Once we have enabled the H2 console, now we can access the H2 console in the browser by invoking the URL http://localhost:8082/h2-console.

How do I test my H2 console?

Access the H2 Console You can access the console at the following URL: http://localhost:8080/h2-console/. You need to enter the JDBC URL, and credentials. To access the test database that the greeter quickstart uses, enter these details: JDBC URL: jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1.


1 Answers

With the reservation that I do not know if there is any tool that can inspect the database, I think that a simple solution would be to use the Spring embedded database (3.1.x docs, current docs) which supports HSQL, H2, and Derby.

Using H2, your xml configuration would look like the following:

<jdbc:embedded-database id="dataSource" type="H2">     <jdbc:script location="classpath:db-schema.sql"/>     <jdbc:script location="classpath:db-test-data.sql"/> </jdbc:embedded-database> 

If you prefer Java based configuration, you can instantiate a DataSource like this (note that EmbeddedDataBase extends DataSource):

@Bean(destroyMethod = "shutdown") public EmbeddedDatabase dataSource() {     return new EmbeddedDatabaseBuilder().             setType(EmbeddedDatabaseType.H2).             addScript("db-schema.sql").             addScript("db-test-data.sql").             build(); } 

The database tables are created by the db-schema.sql script and they are populated with test data from the db-test-data.sql script.

Don't forget to add the H2 database driver to your classpath.

like image 165
matsev Avatar answered Sep 22 '22 17:09

matsev