Better Programming

Advice for programmers.

Follow publication

Member-only story

How To Test Databases Easily in a Spring Boot Application

Kirshi Yin
Better Programming
Published in
4 min readDec 6, 2021
A Mac laptop
Photo by Christopher Gower on Unsplash

Databases are a crucial back-end point of almost any application. Writing good tests helps us check whether the database functionality works as expected before going to production. Fortunately, there are convenient tools that assist us in this task.

In this tutorial, I’m going to show you two ways of testing databases in a Spring Boot application:

You’ll understand the pros and cons of each approach. This will help you make the right decisions when choosing a tool for your database testing.

Let’s get started!

H2 Embedded Database

H2 is a lightweight open-source database based on Java. It provides developers with a quick way to configure and run a database in memory. This way, the data will not be persisted in a real database. It supports multiple SQL compatibility modes, such as IBM DB2, Apache Derby, HSQLDB, MS SQL Server, MySQL, Oracle, and PostgreSQL.

To demonstrate the usage of H2, we’ll create a simple Spring Boot application with a single JPA repository to retrieve users.

In this example, I’m using Maven as a build tool. Create a new Maven project in your favorite IDE, and put the following dependencies in the auto-generated pom.xml file:

Create a new Java class called User:

Note that I’m using the Lombok library to get rid of any boilerplate code, such as getters, setters, etc.

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Kirshi Yin
Kirshi Yin

Written by Kirshi Yin

Self-taught Java Developer. Explores topics about Spring Boot, NLP, Linux, low-coding, and more. Interested in foreign languages, investment, personal growth.

Write a response