Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Testing with H2 db configuration

Tags:

h2

spring-test

I use Oracle in production environment and I would like to use H2 for testing. I can type;

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

so my tables and scripts are created automatically. But I cannot set URL value of this db. (For H2-Oracle compatibility I should add ;MODE=Oracle to url part)

is there a way to achieve this goal?

Or just an opposite solution;

<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>

in this time, I can edit URL part, but how can I load default test scripts (create and data sqls) to this datasource?

like image 671
asyard Avatar asked Nov 17 '11 17:11

asyard


People also ask

How do I access H2 database in Spring?

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:8080/h2-console.

What is the use of H2 database in Spring boot?

Spring Boot & H2 - Overview H2 database is an open source, embedded and in memory relational database management system. It is written in Java and provides a client/server application. It stores data in system memory instead of disk. Once program is closed, data is also lost.

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 check my H2 database 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.


2 Answers

This technique solved the problem;

<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
</bean>

and then adding this tag and definition;

<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
    <jdbc:script location="classpath:testdb/schema.sql" />
    <jdbc:script location="classpath:testdb/data.sql" />
</jdbc:initialize-database>
like image 186
asyard Avatar answered Sep 30 '22 22:09

asyard


Maybe this will help: H2 supports an INIT script (a SQL script which is executed when opening the connection). The database URL would look like this in the XML file:

<property name="url" value="jdbc:h2:file:h2\db;INIT=
RUNSCRIPT FROM 'classpath:schema.sql'\;
RUNSCRIPT FROM 'classpath:test-data.sql'"/>

(the ; needs to be escaped with a backslash).

like image 30
Thomas Mueller Avatar answered Sep 30 '22 21:09

Thomas Mueller