Saturday, 3 November 2012

Integration testing with H2 Db (with maven, hibernate4 and spring)

H2 db is a very fast in memory database engine (like sqlite) written in java. It does not require any external configuration, which makes it ideal for integration testing.

Add the following dependency to your pom
<dependency>
    <groupid>com.h2database</groupId>
    <artifactid>h2</artifactId>
    <version>1.3.168</version>
    <scope>test</scope>
</dependency>
Add the following to your spring context xml file.
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="org.h2.Driver" />

      <!-- where the db will be placed (created automatically) -->
      <property name="url" value="jdbc:h2:target/db/testdb" />
      <property name="username" value="sa" />
      <property name="password" value="" />
</bean>

Add the following lines to your session factory
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="hibernateProperties">
      <props>
        ...
        <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
        <prop key="hibernate.show_sql">true</prop>
        <prop key="hibernate.hbm2ddl.auto">create</prop>
        ...
      </props>
    </property>
    <property name="dataSource" ref="dataSource"/>
</bean>
hibernate.hbm2ddl.auto property above tells hibernate to generate the schema and run it agains the h2 db. Allowing your test to upload the test data and run the tests. See earlier post: Spring-Test-DbUnit on how to set up db tests.

6 comments:

  1. Such an awesome article, these many days, doubts in multiple concepts were bothering me, I started searching through books and net, but doubt was not cleared, thank you so much for providing this article, helped me a lot
    software testing training in chennai

    ReplyDelete
    Replies
    1. The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Final Year Project Centers in Chennai.

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
      Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

      Delete
  2. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    Software testing course in chennai

    ReplyDelete
  3. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    Digital Marketing Training

    ReplyDelete
  4. It was really a nice post and i was really impressed by reading this Big Data Hadoop Online Training Hyderabad

    ReplyDelete
  5. thanks for your information really good and very nice web design company in velachery

    ReplyDelete