PostsRepositoryTest 클래스 생성
PostsRepositoryTest에서는 save, findAll 기능을 테스트함
PostsRepositoryTest 작성
PostsRepositoryTest 코드
package com.jojoldu.book.springboot.domain.posts;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PostsRepositoryTest {
@Autowired
PostsRepository postsRepository;
@After //...1
public void cleanup() {
postsRepository.deleteAll();
}
@Test
public void 게시글저장_불러오기() {
//given
String title = "테스트 게시글";
String content = "테스트 본문";
postsRepository.save(Posts.builder() //...2
.title(title)
.content(content)
.author("[email protected]")
.build());
//when
List<Posts> postsList = postsRepository.findAll(); //...3
//then
Posts posts = postsList.get(0);
assertThat(posts.getTitle()).isEqualTo(title);
assertThat(posts.getContent()).isEqualTo(content);
}
}
@After
postsRepository.save
postsRepository.findAll
별다른 설정이 없이 @SpringBootTest를 사용할 경우 H2 데이터베이스를 자동으로 실행한다.
테스트 실행
실행된 쿼리 로그를 볼 수는 없을까?
쿼리 로그를 ON/OFF 할 수 있는 설정이 있다.
자바 클래스로도 구현 가능하나, 스프링 부트에서는 application.properties 등의 파일로 한 줄의 코드로 설정할 수 있도록 지원한다.
application.properties 생성
우선 src/main/resources 디렉토리 아래에서 Resource Bundle로 만듦
리소스번들은 설정이나 자원을 미리 정의한 후 프로그램 내에서 블러와 사용할 수 있게만 한다.
즉, 프로퍼티 파일을 다룬다.
프로퍼티 파일은 설정이나 자원들에 대해 [키 = 값] 형태로 저장된 파일을 말한다.
application.properties 파일 생성
옵션 추가
spring.jpa.show_sql=true
콘솔에서 쿼리 로그 확인하기
옵션이 잘 적용 되었나?
create table 쿼리를 보면 id bigint generated by default as identity라는 옵션으로 생성된다.
이는 H2의 쿼리 문법이 적용되었기 때문
H2는 MySQL의 쿼리를 수행해도 정상적으로 작동하기에 이후 디버깅을 위해 출력되는 쿼리 로그를 MySQL버전으로 변경해보자.
역시 application.properties에 옵션을 추가한다.
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
다시 테스트 코드 수행 후 옵션 적용 확인