Spring boot with embedded PostgreSQL for DAO integration/unit testing and local development

Here we will go through how to use embedded PostgreSQL in you local development environment plus how to use it for DAO integration/unit testing if you have are using PostgreSQL as your production database , you DAO testing should use the same database as your production one to have behavior consistency and same environment precondition.

We will go through a sample spring boot application to how the needed steps :

overview

  1. The spring boot application dependencies
  2. DB configuration with embedded PostgreSQL for development
  3. Customer crud different layers with spring data , mapstruct and lombok
  4. DAO unit testing with embedded PostgreSQL with custom data population on start

Spring boot dependencies :

the main maven application dependencies will the typical spring boot dependencies (spring data..ect) plus the embedded PostgresSQL and other needed libraries , i will just highlight the embedded PostgresSQL for DB, mapstruct and lombok for DTO and Entity mapping and boilerplate code

Full maven pom.ml is located into the project Github

DB configuration with embedded PostgreSQL for development

So now how to configure the data source to point to the embedded PostgresSQL run-time instance locally , the spring configuration will be as the following :

Customer crud different layers with spring data , mapstruct and lombok

for CUSTOMER crud sample , we will have:

  • Customer entity

  • Customer Spring data repository

  • Customer DTO
  • Customer Map Struct mapper

Again full project code is on Github

DAO unit testing with embedded PostgreSQL with custom data population on start:

  • The unit test DB config , here I intended to to make it a little bit detailed as i need to show how you can load only specific entities , DAOs and Data Repository , as unit testing should be scoped and limited to the target DAO layer only and not to load whole application entities and DAOs, from comments and annotations you will understand how to load specific repositories with their entities only

  • Then finally the Unit test class:

it shows how to load your test configuration , and how to insert some test data before starting the test case using @sql spring jdbc test annotation

Hopefully this help to understand how to do DAO unit testing with custom test-data load and using embedded PostgreSQL

 

References :

  1. Embedded PostgreSQL :https://github.com/yandex-qatools/postgresql-embedded
  2. Map struct : http://mapstruct.org/
  3. Project sample code : https://github.com/Romeh/springboot-postgres-embedded-dao-testing

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s