2022/03/06 BookManager 프로젝트

김석진·2022년 3월 6일
0

BookManager

목록 보기
1/3

프로젝트 세팅

version Spring 2.6.4
Language Java 8(gradle)
Dependencies: Lombok, Spring Web, Spring Data JPA, H2(테스트 데이터베이스)

Hello World 코드 작성 및 테스트 코드 구동 테스트 진행

@RestController
public class HelloWorldController {
    @GetMapping("/hello-world")
    public String helloWorld() {
        return "Hello World";
    }
}

테스트 코드

@WebMvcTest
class HelloWorldControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    void helloWorld() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/hello-world"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string("Hello World"));
    }
}

H2 DB 및 로그 설정

H2 DB

Java 기반의 경량화된 데이터베이스

프로젝트에 세팅하기

application.yml

spring:
  h2:
    console:
      enabled: true

server:
  port: 8070

application을 실행후

이처럼 H2연결이 됐음을 확인할 수 있다
인터넷 브라우저에 해당 로컬 주소/h2-console을 입력 후 Connect를 하면 DB접속이 완료된다

BookManager ERD

JPA Repository Interface 상세

UserRepository 인터페이스 만들기


이렇게 User Entity 와 PK인 ID값의 자료형을 세팅한다

이렇게 인터페이스만 선언 함으로써 JPA관련 메소드를 이용할 수 있다.

H2 DB in Memory를 사용

테스트가 완료되면 데이터가 사라진다.-> 그래서 기본적인 데이터를 사전에 만들어서 조회할때 데이터가 없어지지 않게 만든다.
테스트 폴더 하위에 data.sql를 생성을 한다-> 테스트를 할때만 sql을 실행시켜 데이터를 생성해준다

data.sql

call next value for hibernate_sequence;
insert into user (`id`, `name`, `email`, `created_at`, `updated_at`) values(1,'kim','kimseokjin0324@gmail.com',now(),now());

call next value for hibernate_sequence;
insert into user (`id`, `name`, `email`, `created_at`, `updated_at`) values(2,'lee','lee@gmail.com',now(),now());

call next value for hibernate_sequence;
insert into user (`id`, `name`, `email`, `created_at`, `updated_at`)values(3,'hong','hong@gmail.com',now(),now());

call next value for hibernate_sequence;
insert into user (`id`, `name`, `email`, `created_at`, `updated_at`)values(4,'park','park@gmail.com',now(),now());

call next value for hibernate_sequence;
insert into user (`id`, `name`, `email`, `created_at`, `updated_at`) values(5,'park','park@gmail.com',now(),now());

테스트 코드 를 실행 시키면 아래와 같은 결과가 나온다

profile
주니어 개발자 되고싶어요

0개의 댓글