Skip to content

Feat/Test Container #364

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

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open

Conversation

ChungPlusPlus
Copy link
Contributor

Test Container 추가

한줄 요약

테스트 컨테이너 적용했습니다

상세 설명

testcontainer는 테스트 실행시 컨테이너를 도커로 자동으로 띄워줘 사용할 수 있게 하는 라이브러리입니다
외부 의존성 없이 실제 prod과 유사한 환경에서 테스트 진행이 가능합니다
TestContainerInitializer를 사용해서 테스트가 실행 될 때 환경변수를 바꿔주었습니다

@ContextConfiguration(initializers = [TestContainerInitializer::class])
어노테이션을 적어준다면 MySQL을 이용해 테스트가 진행됩니다

image

[9d3e478]: 테스트 환경에서 getLoginUser()가 2번 이상 실행되면 데이터베이스에 같은 이름의 사람을 2번 이상 저장하게 되어 Unique 때문에 오류가 발생해서 고쳤습니다.

TODO

테스트컨테이너 인스턴스를 하나 만들어서 공유하는 방식이라 한 테스트에서 사용했던 데이터가 남아있다는 문제가 있습니다.
예를 들어서, 공지를 추가하고 공지가 1개 있음을 확인하는 테스트가 2개 있다고 해보겠습니다. 현재는 테스트 하나가 끝나도 데이터베이스에는 데이터가 계속 남아있어 다음 테스트에는 공지가 1개 더 추가되어 2개가 생겨버립니다.
이렇게 테스트 코드끼리 서로 간섭할 수 있는 부분을 해결해야 합니다.

@ChungPlusPlus ChungPlusPlus requested review from leeeryboy and huGgW May 7, 2025 14:33
@ChungPlusPlus ChungPlusPlus self-assigned this May 7, 2025
Copy link

github-actions bot commented May 7, 2025

Test Results

14 files  14 suites   1s ⏱️
59 tests 59 ✔️ 0 💤 0
63 runs  63 ✔️ 0 💤 0

Results for commit 9d3e478.

TestMySQLContainer.instance.start()

TestPropertyValues.of(
"spring.datasource.url=${TestMySQLContainer.instance.jdbcUrl}",
Copy link
Member

Choose a reason for hiding this comment

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

이 부분을 application.yaml에 test 프로파일 값으로 지정해버리는 것은 어떨까요?
추가적으로 다른 테스트들에서도 h2 사용하지 않고 test container를 사용하도록 하는 방식으로요..!

@huGgW
Copy link
Member

huGgW commented May 10, 2025

테스트컨테이너 인스턴스를 하나 만들어서 공유하는 방식이라 한 테스트에서 사용했던 데이터가 남아있다는 문제가 있습니다.

이 부분은 SpringTestLifeCycle 설정을 통해서 해결될 수 있는 것으로 알고 있습니다..! (확실하진 않음) (다른 서비스 레이어 테스트 코드 참고해주세요..!)

@SpringBootTest
@ActiveProfiles("test")
@ContextConfiguration(initializers = [TestContainerInitializer::class])
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
Copy link
Contributor

@leeeryboy leeeryboy May 11, 2025

Choose a reason for hiding this comment

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

TestInstance 어노테이션은 JUnit꺼라 lifecycle이 제대로 적용될지 모르겠네요

추가로 테스트 격리성에 문제가 있으면 kotest 공식 문서랑 아래 블로그 글들 참고해보시면 좋을거 같아요
https://kth990303.tistory.com/374
https://velog.io/@glencode/Kotest%EC%9D%98-Lifecycle-Hook-IsolationMode
testcontainer와 호환이 잘 되는지는 아마 테스트 해봐야할것 같구요

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