From 8550e8910d5f0a0d91c10fc5b235e27de5fe6a2a Mon Sep 17 00:00:00 2001 From: SeongHoonC <108349655+SeongHoonC@users.noreply.github.com> Date: Tue, 24 Oct 2023 17:48:06 +0900 Subject: [PATCH] =?UTF-8?q?[AN/USER]=20refactor:=20ViewModel=20Test=20Code?= =?UTF-8?q?=20Dispatcher=20Rule=20=EC=B6=94=EA=B0=80=20(#590)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: coroutine dispatcher main rule 추가 * refactor: 티켓 예매 화면 ViewModel 테스트 리팩터링 * refactor: 티켓 목록 화면 ViewModel 테스트 리팩터링 * refactor: 티켓 기록 화면 ViewModel 테스트 리팩터링 * refactor: 티켓 입장 화면 ViewModel 테스트 리팩터링 * refactor: 학생 인증 화면 ViewModel 테스트 리팩터링 * refactor: 로그인 화면 ViewModel 테스트 리팩터링 * refactor: 학교 선택 화면 ViewModel 테스트 리팩터링 * refactor: 마이페이지 화면 ViewModel 테스트 리팩터링 * refactor: 홈 화면 ViewModel 테스트 리팩터링 * refactor: 축제 목록 화면 ViewModel 테스트 리팩터링 * refactor: 마이페이지 화면 ViewModel 테스트 재변경 --- .../presentation/rule/MainDispatcherRule.kt | 28 +++++++++++++++++++ .../presentation/ui/home/HomeViewModelTest.kt | 20 ++++--------- .../festivallist/FestivalListViewModelTest.kt | 20 ++++--------- .../ui/home/mypage/MyPageViewModelTest.kt | 20 ++++--------- .../ticketlist/TicketListViewModelTest.kt | 20 ++++--------- .../selectschool/SelectSchoolViewModelTest.kt | 19 ++++--------- .../ui/signin/SignInViewModelTest.kt | 21 ++++---------- .../StudentVerificationViewModelTest.kt | 20 ++++--------- .../ticketentry/TicketEntryViewModelTest.kt | 20 ++++--------- .../TicketHistoryViewModelTest.kt | 21 ++++---------- .../TicketReserveViewModelTest.kt | 20 ++++--------- 11 files changed, 85 insertions(+), 144 deletions(-) create mode 100644 android/festago/app/src/test/java/com/festago/festago/presentation/rule/MainDispatcherRule.kt diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/rule/MainDispatcherRule.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/rule/MainDispatcherRule.kt new file mode 100644 index 000000000..3059da632 --- /dev/null +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/rule/MainDispatcherRule.kt @@ -0,0 +1,28 @@ +package com.festago.festago.presentation.rule + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestDispatcher +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.junit.rules.TestWatcher +import org.junit.runner.Description + +// Reusable JUnit4 TestRule to override the Main dispatcher +class MainDispatcherRule +@OptIn(ExperimentalCoroutinesApi::class) +constructor( + private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher(), +) : TestWatcher() { + + @OptIn(ExperimentalCoroutinesApi::class) + override fun starting(description: Description) { + Dispatchers.setMain(testDispatcher) + } + + @OptIn(ExperimentalCoroutinesApi::class) + override fun finished(description: Description) { + Dispatchers.resetMain() + } +} diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/HomeViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/HomeViewModelTest.kt index 6a7c5616c..b867fe986 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/HomeViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/HomeViewModelTest.kt @@ -1,38 +1,30 @@ package com.festago.festago.presentation.ui.home import app.cash.turbine.test +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.AuthRepository import io.mockk.every import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.Assertions.assertThat -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test class HomeViewModelTest { + private lateinit var vm: HomeViewModel private lateinit var authRepository: AuthRepository - @OptIn(ExperimentalCoroutinesApi::class) + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(UnconfinedTestDispatcher()) authRepository = mockk() vm = HomeViewModel(authRepository) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `사용자 인증 유무가 다음과 같을 때`(isSigned: Boolean) { every { authRepository.isSigned } returns isSigned } diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/festivallist/FestivalListViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/festivallist/FestivalListViewModelTest.kt index 56b5b060b..212b203c1 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/festivallist/FestivalListViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/festivallist/FestivalListViewModelTest.kt @@ -3,20 +3,16 @@ package com.festago.festago.presentation.ui.home.festivallist import app.cash.turbine.test import com.festago.festago.analytics.AnalyticsHelper import com.festago.festago.model.Festival +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.FestivalRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.SoftAssertions -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test import java.time.LocalDate @@ -36,21 +32,16 @@ class FestivalListViewModelTest { ) } - @OptIn(ExperimentalCoroutinesApi::class) + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(UnconfinedTestDispatcher()) festivalRepository = mockk() analyticsHelper = mockk(relaxed = true) vm = FestivalListViewModel(festivalRepository, analyticsHelper) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `축제 목록 요청 결과가 다음과 같을 때`(result: Result>) { coEvery { festivalRepository.loadFestivals() @@ -131,7 +122,6 @@ class FestivalListViewModelTest { @Test fun `티켓 예매를 열면 티켓 예매 열기 이벤트가 발생한다`() = runTest { - vm.event.test { // when val fakeFestivalId = 1L diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/mypage/MyPageViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/mypage/MyPageViewModelTest.kt index 2eb87bdae..a93a2d8aa 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/mypage/MyPageViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/mypage/MyPageViewModelTest.kt @@ -7,26 +7,23 @@ import com.festago.festago.model.Stage import com.festago.festago.model.Ticket import com.festago.festago.model.TicketCondition import com.festago.festago.model.UserProfile +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.AuthRepository import com.festago.festago.repository.TicketRepository import com.festago.festago.repository.UserRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.SoftAssertions -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test import java.time.LocalDateTime class MyPageViewModelTest { + private lateinit var vm: MyPageViewModel private lateinit var userRepository: UserRepository private lateinit var ticketRepository: TicketRepository @@ -55,10 +52,11 @@ class MyPageViewModelTest { ), ) - @OptIn(ExperimentalCoroutinesApi::class) + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(UnconfinedTestDispatcher()) userRepository = mockk(relaxed = true) ticketRepository = mockk() authRepository = mockk() @@ -66,12 +64,6 @@ class MyPageViewModelTest { vm = MyPageViewModel(userRepository, ticketRepository, authRepository, analyticsHelper) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `로그인 상태가 다음과 같다`(result: Boolean) { coEvery { authRepository.isSigned diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/ticketlist/TicketListViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/ticketlist/TicketListViewModelTest.kt index 050fe50fd..2ddeb69f5 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/ticketlist/TicketListViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/home/ticketlist/TicketListViewModelTest.kt @@ -4,41 +4,33 @@ import app.cash.turbine.test import com.festago.festago.analytics.AnalyticsHelper import com.festago.festago.model.Ticket import com.festago.festago.presentation.fixture.TicketFixture +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.TicketRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.SoftAssertions -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test class TicketListViewModelTest { + private lateinit var vm: TicketListViewModel private lateinit var ticketRepository: TicketRepository private lateinit var analyticsHelper: AnalyticsHelper - @OptIn(ExperimentalCoroutinesApi::class) + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(UnconfinedTestDispatcher()) ticketRepository = mockk() analyticsHelper = mockk(relaxed = true) vm = TicketListViewModel(ticketRepository, analyticsHelper) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `현재 티켓 요청 결과가 다음과 같을 때`(result: Result>) { coEvery { ticketRepository.loadCurrentTickets() } returns result } diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/selectschool/SelectSchoolViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/selectschool/SelectSchoolViewModelTest.kt index d6708dc56..a898f407c 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/selectschool/SelectSchoolViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/selectschool/SelectSchoolViewModelTest.kt @@ -3,32 +3,28 @@ package com.festago.festago.presentation.ui.selectschool import app.cash.turbine.test import com.festago.festago.analytics.AnalyticsHelper import com.festago.festago.model.School +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.SchoolRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.SoftAssertions.assertSoftly -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test -@OptIn(ExperimentalCoroutinesApi::class) class SelectSchoolViewModelTest { - private val testDispatcher = UnconfinedTestDispatcher() private lateinit var vm: SelectSchoolViewModel private lateinit var schoolRepository: SchoolRepository private lateinit var analyticsHelper: AnalyticsHelper + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setup() { - Dispatchers.setMain(testDispatcher) schoolRepository = mockk() analyticsHelper = mockk(relaxed = true) vm = SelectSchoolViewModel( @@ -37,11 +33,6 @@ class SelectSchoolViewModelTest { ) } - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `학교 목록 불러오기 요청 결과가 다음과 같을 때 `(result: Result>) { coEvery { schoolRepository.loadSchools() diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/signin/SignInViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/signin/SignInViewModelTest.kt index c8f209389..5a53f69b5 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/signin/SignInViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/signin/SignInViewModelTest.kt @@ -2,41 +2,32 @@ package com.festago.festago.presentation.ui.signin import app.cash.turbine.test import com.festago.festago.analytics.AnalyticsHelper +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.AuthRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.Assertions.assertThat -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test class SignInViewModelTest { + private lateinit var vm: SignInViewModel private lateinit var authRepository: AuthRepository private lateinit var analyticsHelper: AnalyticsHelper - @OptIn(ExperimentalCoroutinesApi::class) + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(UnconfinedTestDispatcher()) - authRepository = mockk(relaxed = true) analyticsHelper = mockk(relaxed = true) vm = SignInViewModel(authRepository, analyticsHelper) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `로그인 결과가 다음과 같을 때`(result: Result) { coEvery { authRepository.signIn() } returns result } diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/studentverification/StudentVerificationViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/studentverification/StudentVerificationViewModelTest.kt index 672066619..2dd025261 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/studentverification/StudentVerificationViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/studentverification/StudentVerificationViewModelTest.kt @@ -3,34 +3,30 @@ package com.festago.festago.presentation.ui.studentverification import app.cash.turbine.test import com.festago.festago.analytics.AnalyticsHelper import com.festago.festago.model.StudentVerificationCode +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.SchoolRepository import com.festago.festago.repository.StudentVerificationRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.SoftAssertions.assertSoftly -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test -@OptIn(ExperimentalCoroutinesApi::class) class StudentVerificationViewModelTest { - private val testDispatcher = UnconfinedTestDispatcher() private lateinit var vm: StudentVerificationViewModel private lateinit var studentVerificationRepository: StudentVerificationRepository private lateinit var schoolRepository: SchoolRepository private lateinit var analyticsHelper: AnalyticsHelper + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(testDispatcher) studentVerificationRepository = mockk() schoolRepository = mockk() analyticsHelper = mockk(relaxed = true) @@ -41,12 +37,6 @@ class StudentVerificationViewModelTest { ) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `이메일 요청 결과가 다음과 같을 때`(result: Result) { coEvery { schoolRepository.loadSchoolEmail(any()) diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/ticketentry/TicketEntryViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/ticketentry/TicketEntryViewModelTest.kt index 9369a2a87..356b8af93 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/ticketentry/TicketEntryViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/ticketentry/TicketEntryViewModelTest.kt @@ -4,40 +4,32 @@ import com.festago.festago.analytics.AnalyticsHelper import com.festago.festago.model.Ticket import com.festago.festago.model.TicketCode import com.festago.festago.presentation.fixture.TicketFixture +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.TicketRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.SoftAssertions -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test class TicketEntryViewModelTest { + private lateinit var vm: TicketEntryViewModel private lateinit var ticketRepository: TicketRepository private lateinit var analyticsHelper: AnalyticsHelper - @OptIn(ExperimentalCoroutinesApi::class) + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(UnconfinedTestDispatcher()) ticketRepository = mockk() analyticsHelper = mockk(relaxed = true) vm = TicketEntryViewModel(ticketRepository, analyticsHelper) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `티켓 요쳥 결과는 다음과 같을 때`(result: Result) { coEvery { ticketRepository.loadTicket(any()) } returns result } diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/tickethistory/TicketHistoryViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/tickethistory/TicketHistoryViewModelTest.kt index b9c8f8fec..5611f58fe 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/tickethistory/TicketHistoryViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/tickethistory/TicketHistoryViewModelTest.kt @@ -3,30 +3,27 @@ package com.festago.festago.presentation.ui.tickethistory import com.festago.festago.analytics.AnalyticsHelper import com.festago.festago.model.Ticket import com.festago.festago.presentation.fixture.TicketFixture +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.TicketRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.setMain import org.assertj.core.api.SoftAssertions -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test class TicketHistoryViewModelTest { - private lateinit var vm: TicketHistoryViewModel + private lateinit var vm: TicketHistoryViewModel private lateinit var ticketRepository: TicketRepository private lateinit var analyticsHelper: AnalyticsHelper - @OptIn(ExperimentalCoroutinesApi::class) + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(UnconfinedTestDispatcher()) ticketRepository = mockk() analyticsHelper = mockk(relaxed = true) @@ -36,12 +33,6 @@ class TicketHistoryViewModelTest { ) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun finish() { - Dispatchers.resetMain() - } - private fun `티켓 기록 요청 결과가 다음과 같을 때`(result: Result>) { coEvery { ticketRepository.loadHistoryTickets(any()) diff --git a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/ticketreserve/TicketReserveViewModelTest.kt b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/ticketreserve/TicketReserveViewModelTest.kt index b6a5582c7..d6cda5639 100644 --- a/android/festago/app/src/test/java/com/festago/festago/presentation/ui/ticketreserve/TicketReserveViewModelTest.kt +++ b/android/festago/app/src/test/java/com/festago/festago/presentation/ui/ticketreserve/TicketReserveViewModelTest.kt @@ -8,28 +8,25 @@ import com.festago.festago.model.ReservationTicket import com.festago.festago.model.ReservationTickets import com.festago.festago.model.ReservedTicket import com.festago.festago.model.TicketType +import com.festago.festago.presentation.rule.MainDispatcherRule import com.festago.festago.repository.AuthRepository import com.festago.festago.repository.FestivalRepository import com.festago.festago.repository.ReservationTicketRepository import com.festago.festago.repository.TicketRepository import io.mockk.coEvery import io.mockk.mockk -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay -import kotlinx.coroutines.test.UnconfinedTestDispatcher -import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain import org.assertj.core.api.AssertionsForClassTypes.assertThat import org.assertj.core.api.SoftAssertions -import org.junit.After import org.junit.Before +import org.junit.Rule import org.junit.Test import java.time.LocalDate import java.time.LocalDateTime class TicketReserveViewModelTest { + private lateinit var vm: TicketReserveViewModel private lateinit var reservationTicketRepository: ReservationTicketRepository private lateinit var festivalRepository: FestivalRepository @@ -66,10 +63,11 @@ class TicketReserveViewModelTest { number = 1, ) - @OptIn(ExperimentalCoroutinesApi::class) + @get:Rule + val mainDispatcherRule = MainDispatcherRule() + @Before fun setUp() { - Dispatchers.setMain(UnconfinedTestDispatcher()) reservationTicketRepository = mockk() festivalRepository = mockk() ticketRepository = mockk() @@ -84,12 +82,6 @@ class TicketReserveViewModelTest { ) } - @OptIn(ExperimentalCoroutinesApi::class) - @After - fun tearDown() { - Dispatchers.resetMain() - } - private fun `예약 정보 요청 결과가 다음과 같을 때`(result: Result) { coEvery { festivalRepository.loadFestivalDetail(any()) } returns result }