2 분 소요


Connecting to a Database with SpringBoot - H2

Before using MySQL, a commonly used database, I’ll use the H2 database for testing purposes, write some simple logic, and run tests.

Managing SpringBoot Packages

I plan to manage packages with Gradle. Previously, I used Maven for management, but after using both, I felt that Gradle is more intuitive and convenient. (Actually, as long as the package names are the same, it probably doesn’t matter.)

I configured the build.gradle file as follows:

SpringBoot build dependency setting - build.gradle

Since the dependency configuration is complete, let’s add the configuration values so that SpringBoot runs properly.

The first file you can configure is application.properties, but I created a new application.yml to express hierarchy and make it easier to read.

SpringBoot Application Configuration - application.yml

Now that the setup is complete, let’s create a simple application.

Creating the Model

To insert data, we need to create a table in H2 where data can be stored and specify the data format.

I created a model package inside the base package and created a UserModel inside it.

usermodel.png

The model will represent the most basic User elements. Based on this, I think we can later expand to various services like login/logout/registration.

Creating the Repository

Let’s create a Repository with the User Model we created above. Since I’m just trying to test with a slightly improved basic Save Method, no major work is needed.

repostory.png

I indicated the repository with the @Repository annotation, inherited from JpaRepository to use basic methods, and created a findByName method to search by username.

Creating the Test Class (Junit4)

Let’s do a simple test with the Repository created above. I’m going to test CRUD with the User model.

Create is about generating data, so we can test whether the table creation and Repository were written correctly. Therefore, there shouldn’t be much need for preconditions.

However, Read, Update, and Delete are processes of finding, reading, modifying, and deleting data, so the presence of data retrieved by the Repository function is important.

This time, I’ll apply execution order to the test code to test the subsequent operations after creating the data.

I’m using Junit4 here. The features I want to use have been made more convenient in Junit5. I plan to cover that again later.

First, create a class to test and set up the overall class structure for testing.

Creating SpringBoot Test class for writing test functions

Connect the Repository with the @Autowired annotation.

I’m going to apply a new @FixMethodOrder annotation to the Test Class to enable ordering for each test function. I’ve given it the NAME_ASCENDING option for now, but there are other options as well.

Test class with FixMethodOrder annotation applied

As you can tell from the NAME_ASCENDING option, it sorts according to test function names. I numbered the function names and roughly indicated what each function does.

test-function.png

Test Function - Create

test-create.png

First, let’s verify this carefully since this data will be used in Read, Update, and Delete later.

Test Function - Read

test-read.png

Test Function - Update

test-update.png

Test Function - Delete

test-delete.png

댓글남기기