-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
테스트 시 DB 격리 및 레포지토리 테스트를 위한 베이스 어노테이션 생성 #529
Conversation
@DataJpaTest | ||
@DatabaseIsolation | ||
@Import(JpaAuditingConfiguration.class) | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
코드가 낯설까봐 추가 코멘트 달아요 🔥
이해 안되시는 부분 있다면 말해주세요 ⚡️
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) 는 테스트용 인메모리 데이터베이스가 아닌 실제 설정된 데이터베이스를 사용하도록 합니다.
@DataJpaTest
이 실행될 때 스프링은 기본적으로 테스트 시 인메모리 데이터베이스 (기존 h2를 사용했을 때처럼 우리 컴퓨터의 메모리에 데이터를 저장하는 DB)로 테스트를 실행 시킵니다.
따라서 우리 테스트 환경인 mysql로 테스트 데이터베이스를 사용하려면 해당 어노테이션을 달아줘야 합니당
import org.springframework.test.context.TestExecutionListeners; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@TestExecutionListeners(value = DatabaseCleaner.class, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 우리가 만든 DatabaseCleaner 클래스를 테스트 실행 리스너로 등록한다는 의미입니다.
mergeModes의 MERGE_WITH_DEFAULTS
는 우리가 추가한 DatabaseCleaner를 기존 스프링의 기본 리스너들과 함께 사용하겠다는 의미입니다.
(이 모드를 사용하지 않으면, 기본 스프링의 리스너들이 모두 무시되고 DatabaseCleaner만 사용되게 됩니다.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
친절한 설명 고마워요 몰리 👍
truncate(queries, jdbcTemplate); | ||
} | ||
|
||
private List<String> getTruncateQueries(final JdbcTemplate jdbcTemplate) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
private List<String> getTruncateQueries(final JdbcTemplate jdbcTemplate) { | |
private List<String> getTruncateQueries(JdbcTemplate jdbcTemplate) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
몰리 짱 수고했어용 ~~ 👍❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
어려운걸 또 찾아주셨네요.
고생하셨습니다 몰리.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR 본문과 링크된 블로그 글 덕분에 수월하게 이해할 수 있었습니다!
고생했어요 몰리!!!
⚡️ 관련 이슈
close #469
📍주요 변경 사항
@DatabaseIsolation
생성@DatabaseIsolation
어노테이션을 부착하게 되면 매 테스트 시 DatabaseCleaner가 동작합니다.@JpaRepositoryTest
생성🎸기타
앞으로 매 레포지토리 테스트 시 해당 어노테이션을 사용하면 편하실 듯~