10개 프로젝트로 완성하는 백엔드 웹개발(Java/Spring) 초격차 패키지 Online. part2 : 03.데이터베이스 접근 로직 테스트 정의1~4
A.
show databases;
create database board;
create user 'DuckCow'@'localhost' identified by 'thisisTESTpw!#%&';
select `user` from `mysql`.`user`;
show grants for 'DuckCow'@'localhost';
grant all on `board`.* to 'DuckCow'@'localhost' with grant option;
flush privileges;
A.1
권한 확인 후, 권한 부여
datagrip은 'root' 로 접근하고 applicaiton 은 'DuckCow'@'localhost' 로 접근
B.
protected Article(){}
private Article(String title, String content, String hashtag) {
this.title = title;
this.content = content;
this.hashtag = hashtag;
}
public static Article of(String title, String content, String hashtag){
return new Article(title,content,hashtag);
}
@Id 인 id는 generated value로 설정하고, 메타데이터인 나머지 데이터는 자동으로 들어가게 할 것으로
나머지만 생성자로 만든다. 이때, private으로 다른 접근을 막고, public static Article of 로 해당 Article domain에 접근할때는 해당 파라미터가 필요하다는 의도를 전달한다.
B.2 - @Equals and @Hashcode
두 개의 동등성을 확인하기 위해서는 @id 만 비교하면 되기 때문에, @lombok을 사용하지 않고 직접 명령어 통해 만들어준다. 이때, id이기 때문에 not null로 생성해준다.
part2 : 03.데이터베이스 접근 로직 테스트 정의 2
C. intellij 'Service' 기능
- run 쪽의 log와 분리하여 service log를 따로 볼 수 있음
- 멀티 모듈 프로젝트를 하나로 관리할 수 있다.
D.
@ToString.Exclude
@OrderBy("id")
@OneToMany(mappedBy = "article",cascade = CascadeType.ALL)
private final Set<ArticleComment> articleComments = new LinkedHashSet<>();
D.1 지금은 comment와 article을 양방향으로 묶고, cascade했지만, 실무에서는 양방향을 잘 사용하지 않음.
퍼포먼스 측면이나, 한쪽이 삭제됐음에도 나머지를 백업하기 위해 보존하는 경우도 있기 때문에.
D.2 순환 참조를 막기위해, article. comment 쪽에 @ToString.Exclude를 추가. 게시글이 사라지면, 댓글이 사라지는게 자연스럽기 때문.
4.
E.
@DataJpaTest
class JpaRepositoryTest {
@DataJpaTest로 인해 기본적으로 @Transactional이 걸려있다.
그렇기때문에 rollback이 발생하여 update 쿼리가 발생하지 않았고, 직접적으로 flush를 통해 update 쿼리를 날린다.
saveandFlush를 통해 쿼리를 날리나, 결국 rollback이 발생하여 반영되지는 않는다.
F. Testing properties
Testing properties 설정을 통해, test시 test db만 사용하도록 할 수 있고, 또 다른 경우 실제 db를 test에 사용하도록 설정할 수 있다.