Spring Boot JPA Test Project 생성

JPA 관련 테스트를 진행하기 위해 프로젝트를 만드는 내용을 정리했습니다.

OpenJDK 17 설치

작성자는 MacOS를 사용중입니다. brew를 활용해서 설치하겠습니다.

Brew Install OpenJDK 17

brew install openjdk@17

시스템 등록 (심볼릭 링크)

sudo ln -sfn /usr/local/opt/openjdk@17/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-17.jdk

테스트 데이터베이스 생성

이해를 돕기 위해 내용을 작성했지만 데이터베이스명테이블명 또는 유저명, 유저비밀번호는 직접 다르게 설정하셔도 무관합니다. 자세한 설명을 보고 싶으면 PostgreSQL 데이터베이스 생성에서 참조하시면 됩니다.

데이터베이스 생성

create database jpa_db;

유저 생성

create user jpa_user with encrypted password 'jpa_password';

데이터베이스 유저 전체권한 설정

grant all privileges on database jpa_db to jpa_user;

Spring Boot 3.2 Maven 프로젝트 생성

application.properties 설정

JPA

spring.jpa.database=postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.jdbc.time_zone=Asia/Seoul
spring.jpa.show-sql=true

PostgreSQL

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/jpa_db
spring.datasource.username=jpa_user
spring.datasource.password=jpa_password

Flyway

spring.flyway.baselineOnMigrate = true
flyway.url=jdbc:postgresql://localhost:5432/jpa_db
flyway.user=jpa_user
flyway.password=jpa_password

전문

# JPA
spring.jpa.database=postgresql
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.generate-ddl=false
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.jdbc.time_zone=Asia/Seoul
spring.jpa.show-sql=true

# PostgreSQL
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/jpa_db
spring.datasource.username=jpa_user
spring.datasource.password=jpa_password

# Flyway
spring.flyway.baselineOnMigrate = true
flyway.url=jdbc:postgresql://localhost:5432/jpa_db
flyway.user=jpa_user
flyway.password=jpa_password

Flyway 테이블 생성

프로젝트 생성 시 Flyway를 선택했다면 src/main/resources/db.migration이란 폴더가 있을 겁니다. V1__init.sql 파일 만들어서 테이블 생성하는 SQL문을 작성하겠습니다.

자세한 Flyway 사용방법을 알고 싶으시면 Flyway 사용법에서 확인 가능합니다.

V1__init.sql

DROP TABLE IF EXISTS users;
CREATE TABLE users (
    id                  bigserial primary key,
    created_at          timestamp,
    updated_at          timestamp,
    deleted_at          timestamp,
    email               varchar(254),
    hashed_password     varchar(254),
    name                varchar(64)
);

DROP TABLE IF EXISTS orders;
CREATE TABLE orders (
    id                  bigserial primary key,
    created_at          timestamp,
    updated_at          timestamp,
    deleted_at          timestamp,
    user_id             bigint
);

User.class 생성

Spring Security

처음에는 JPA 테스트만 하기위해서 프로젝트를 생성했으나 실제 프로젝트와 유사한 환경을 만들고 싶어서 스프링 시큐리티를 추가하기로 했다.

pom.xml 스프링 스타터 시큐리티 추가

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

JUnit