version Spring 2.6.4
Language Java 8(gradle)
Dependencies: Lombok, Spring Web, Spring Data JPA, H2(테스트 데이터베이스)
@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"));
}
}
Java 기반의 경량화된 데이터베이스
spring:
h2:
console:
enabled: true
server:
port: 8070
application을 실행후
이처럼 H2연결이 됐음을 확인할 수 있다
인터넷 브라우저에 해당 로컬 주소/h2-console을 입력 후 Connect를 하면 DB접속이 완료된다
이렇게 User Entity 와 PK인 ID값의 자료형을 세팅한다
이렇게 인터페이스만 선언 함으로써 JPA관련 메소드를 이용할 수 있다.
테스트가 완료되면 데이터가 사라진다.-> 그래서 기본적인 데이터를 사전에 만들어서 조회할때 데이터가 없어지지 않게 만든다.
테스트 폴더 하위에 data.sql를 생성을 한다-> 테스트를 할때만 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());
테스트 코드 를 실행 시키면 아래와 같은 결과가 나온다