Skip to content

Conversation

@tryterry77
Copy link

게시글 레포 테스트 입니다.

게시글(Post)과 User, Board의 연관 관계에 따라 생성 과정에서 발생하는 문제를 테스트합니다.

BeforeEach를 사용하는 방법대신 객체별로 generate() 메소드를 만들고,

각 테스트 항목마다 초기화하는 방법으로 진행해봤습니다.

이렇게 할 경우 BeforeEach에서 테스트 항목 마다 분기 처리하지 않고,

테스트 항목 안에서 어떤 준비과정이 필요한지 더 가독성을 높일 수 있다고 생각합니다.

@tryterry77 tryterry77 self-assigned this Nov 13, 2025
Comment on lines +1 to +13
## h2 설정
spring:
# H2 Setting Info (H2 Console에 접속하기 위한 설정정보 입력)
h2:
console:
enabled: true # H2 Console을 사용할지 여부 (H2 Console은 H2 Database를 UI로 제공해주는 기능)
path: /h2-console # H2 Console의 Path
# Database Setting Info (Database를 H2로 사용하기 위해 H2연결 정보 입력)
datasource:
driver-class-name: org.h2.Driver # Database를 H2로 사용하겠다.
url: jdbc:h2:~/test # H2 접속 정보
username: sa # H2 접속 시 입력할 username 정보 (원하는 것으로 입력)
password: # H2 접속 시 입력할 password 정보 (원하는 것으로 입력) No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 올리면 음 db랑 테이블 수동으로 만드셨습니까?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

음.. 아뇨 Jpa 사용하고 있으니 h2로 교체해도 자동으로 테이블 생성되지 않습니까

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오호 이렇게해도 jpa 생성됩니까?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 jpa 설정이 빠졌는데 생성되는지 물어보신 거라면

아마 jpa ddl-auto 디폴트가 create라서 자동 생성되지 않을까 합니다..?

"title_" + UUID.randomUUID(),
"content_" + UUID.randomUUID(),
PostDisclosureType.PUBLIC,
null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이부분과 아래 createdBy null 로 했을때까지의 테스트까지 해버리면 너무 광범위 해버리게 될것같네요

이런 다른 참조하는 객체들과의 문제는 서비스에 방어코드를 넣을테고 서비스 테스트로 통합하죠!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post와 연관관계에 있는 Board와 User가 null 일 경우 모두 체크하다보니 그렇게 된 것 같습니다.

서비스 혹은 도메인에서 다른 참조 객체와의 관계까지 검증해야하는지 아직 그 범위가 명확하게 와닿지 않고,

아무래도 repository에서 테스트는 직접 db에 데이터를 저장하고 조회하는 테스트가 필요할것 같아서

우선은 이런식으로 테스트를 진행하긴 했습니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 제 생각은 Board 순수 필드들만 검증하고, 나머지 참조 객체들은 서비스에서 validation 하죠!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그렇다면 repository 테스트도 크게 필요하지 않을 것 같습니다.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

일단 해본거긴한데 저도 repository 테스트는 그렇게 필요할것같지 않아보입니다

@@ -9,7 +9,7 @@

public class PostGenerator {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostGenerator 라는 임의 PostEntlty 동일 구조를 갖고있는 클래스를 만들어서 표현한거라면

어차피 테스트 소스고 db에도 저장되지 않는 내용이라

기존 PostEntity 를 이용해버심은 어떨까요!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PostGenerator의 역할은 PostEntity를 생성할때 기존에 일일이 필드를 입력하는 번거로움을 해결하기 위해

자동으로 임의의 객체를 만들어주는 팩토리 클래스라고 생각하시면 됩니다.

코드 내용을 보시면 알겠지만, 제목, 내용 등을 랜덤하게 생성해줍니다.

실제 사용할때는 그냥 단순히 generatePost() 메소드만 호출하면 되는거죠.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

따지고보면 PostEntity 에 대한 테스트를 하려고 하는거니 기존 PostEntity 자체만으로 테스트해보심이 나을듯해보이긴합니다!! 하지만 시도는 굿입니다

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생각해보니 그러네요 ㅋㅋ 사실 이 generator는 컨트롤러 통합 테스트에서 requestBody 만들때 사용했던 것 같습니다. 헷갈렷네요 ㅜ

Comment on lines +92 to +95
PostEntity post = generatePost(board, user);
// when
PostEntity saved = postRepository.save(post);
// then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generatePost 한 PostEntity post 를 바로 save 하면 영속성 객체로 변하게되어 PostEntity saved 굳이 또 만들 필요 없으실듯합니다.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 맞네요 ㅜ

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id 값을 비교해서 검증해야 하나 싶어서 saved로 받으려고 했던것 같습니다 수정할게요

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 생성하고 조회하는 과정에서 findById에서 id값이 필요해 saved에서 id를 가져왔네요..

saved가 필요할것 같습니다

// when
PostEntity saved = postRepository.save(post);
// then
Optional<PostEntity> found = postRepository.findById(saved.getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found 말고 더 명확하게 foundedPost 는 어떨까욤

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found는 find의 과거분사로 '발견된' 의미이고, founded는 found(설립하다)의 과거분사로 '설립된'이라는 의미로 알고 있습니다만..?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foundPost로 바꾸면 될까요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants