Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot testing using in memory db

I have created a Spring web project using Spring Boot. Would like to understand the practice around testing. I require an in memory embedded database say hsql or h2 for my junits with initial schema.sql. And on the main application the database could be say mysql or oracle

In a non Spring Boot project, we would normally have a separate applicationcontext.xml one which is referred by the web app and for testing we would use applicationContext-text.xml

Now, in Spring boot as everything is created automatically and Spring Boot is opiniated too. Would like to know how do I setup having an embedded inmemory db for Junits and an external db like MySQL for the application.

One solution I can think of is using Profiles. with 2 properties file application.properties and application-test.properties. and use test profile for my junits.

Any recommendation on the approach I should take.

like image 349
Nehal Damania Avatar asked Oct 26 '13 05:10

Nehal Damania


People also ask

What is in memory DB in spring boot?

The in-memory database is an embedded database. The in-memory databases are volatile, by default, and all stored data loss when we restart the application. The widely used in-memory databases are H2, HSQLDB (HyperSQL Database), and Apache Derby. It creates the configuration automatically.

What DB should I use for spring boot?

Spring Boot gives you defaults on all things. For example, the default database is H2 . Consequently, when you want to use any other database, you must define the connection attributes in the application. properties file.

How do you know if a database connection is successful in spring boot?

The easiest way to test the database connection from Spring boot is to start the application and by checking to debug logs.

Is hibernate in-memory database?

In this Hibernate HSQLDB database tutorial, you will learn how to create a Hibernate Application to connect the HSQLDB in-memory database. Hibernate is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.


1 Answers

A profile is, indeed, the recommended approach. What I would do is probably make the in-memory implementation the "default" profile (it's harmless, in the sense that you never change any real data, so it's better to make that the default in case someone accidentally runs it against a real database). Personally, I prefer to put all the external configuration in a single application.yml file, but that's really up to you. In the external configuration you need to supply a valid driver class and URL, e.g.

spring:   datasource:     driverClassName: org.h2.Driver     url: jdbc:h2:mem:test;MODE=PostgreSQL     schema: classpath:/schema.sql  ---  spring:   profiles: local   datasource:     url: jdbc:postgresql://localhost/test     username: root     password: changeme     driverClassName: org.postgresql.Driver     schema: 

(Note that H2 has a postgres compatibility mode, so it is really nice as a complement to postgres in production.)

like image 193
Dave Syer Avatar answered Sep 29 '22 11:09

Dave Syer