-
Notifications
You must be signed in to change notification settings - Fork 1
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
테스트 코드 작성 환경 설정 #85
테스트 코드 작성 환경 설정 #85
Conversation
Quality Gate passedIssues Measures |
@Transactional | ||
@SpringBootTest | ||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
public @interface ServiceTest { |
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.
수정에 대한 이유도 설명해주셔야할 것 같아요.
public void beforeEach() { | ||
final List<String> truncateQueries = getTruncateQueries(jdbcTemplate); | ||
execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY FALSE"); | ||
truncateTables(jdbcTemplate, truncateQueries); | ||
execute(jdbcTemplate, "SET REFERENTIAL_INTEGRITY TRUE"); | ||
} | ||
|
||
private List<String> getTruncateQueries(final JdbcTemplate jdbcTemplate) { | ||
return jdbcTemplate.queryForList("SELECT Concat('TRUNCATE TABLE ', TABLE_NAME, ' RESTART IDENTITY;') AS q FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'PUBLIC'", String.class); | ||
} | ||
|
||
private void truncateTables(final JdbcTemplate jdbcTemplate, final List<String> truncateQueries) { | ||
truncateQueries.forEach(v -> execute(jdbcTemplate, v)); | ||
} | ||
|
||
private void execute(final JdbcTemplate jdbcTemplate, final String query) { | ||
jdbcTemplate.execute(query); | ||
} | ||
} |
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.
truncate 처리를 코드에서 진행하기 위한 함수입니다
- 먼저 REFERENTIAL_INTEGRITY는 외래키 제약조건 설정입니다.
- 테이블을 순서에 상관없이 truncate 하기 위해 끈 상태에서
- INFORMATION_SCHEMA.TABLES라는 곳에서 테이블 이름을 모두 가져옵니다
- 그 테이블 이름을 가져옴과 동시에 TRUNCATE TABLE TABLE_NAME RESTART IDENTITY 라는 문자열 형태로 가져옵니다
- forEach 형태를 통해 각 테이블 truncate를 실행합니다
- 다시 외래키 제약조건을 켭니다
위와같은 형태로 테이블 개수에 상관없이 모든 테이블을 truncate하는 것을 코드 형태로 작성된 클래스입니다
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.
굳굳!
@transactional 어노테이션을 사용하는 상황
truncate를 사용해야 하는 상황