Skip to content

Commit

Permalink
GithubViewModel 테스트코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
KwonDae committed Sep 1, 2023
1 parent 5bf2443 commit 7e7887b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 17 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ dependencies {
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.6.1")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")

// test
testImplementation("junit:junit:4.13.2")
testImplementation("com.google.truth:truth:1.1.3")
testImplementation("androidx.arch.core:core-testing:2.2.0")
testImplementation("androidx.test:core-ktx:1.5.0")
testImplementation("io.mockk:mockk:1.13.7")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3")
}
17 changes: 0 additions & 17 deletions app/src/test/java/camp/nextstep/edu/github/ExampleUnitTest.kt

This file was deleted.

94 changes: 94 additions & 0 deletions app/src/test/java/camp/nextstep/edu/github/GithubViewModelTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* @author Daewon on 02,September,2023
*
*/

package camp.nextstep.edu.github

import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import camp.nextstep.edu.github.domain.model.GithubRepository
import camp.nextstep.edu.github.domain.repository.NetworkRepository
import com.google.common.truth.Truth.assertThat
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test


class GithubViewModelTest {

@get:Rule
val mainDispatcherRule = MainDispatcherRule()

@Rule
@JvmField
val instantExecutorRule = InstantTaskExecutorRule()

private lateinit var viewModel: GithubViewModel
private val networkRepository: NetworkRepository = mockk(relaxed = true)

@Test
fun `GithubViewModel이 생성되면, 정상적으로 통신할 경우 GithubRepository를 가져온다`() = runTest {
// given
coEvery { networkRepository.getRepositories() } returns Result.success(
listOf(
GithubRepository(
fullName = "KwonDae",
description = "KwonDae's repository",
)
)
)

// when
viewModel = GithubViewModel(networkRepository)

// then
coVerify { networkRepository.getRepositories() }
assertThat(viewModel.uiState.value).isEqualTo(
UiState.Success(
listOf(
GithubRepository(
fullName = "KwonDae",
description = "KwonDae's repository",
)
)
)
)
}

@Test
fun `GithubViewModel이 생성되고, 정상적으로 통신했으나 빈값인 경우, 빈화면이 노출되어야 한다`() = runTest {
// given
coEvery { networkRepository.getRepositories() } returns Result.success(emptyList())

// when
viewModel = GithubViewModel(networkRepository)

// then
coVerify { networkRepository.getRepositories() }
assertThat(viewModel.uiState.value).isEqualTo(
UiState.Success(emptyList())
)
}

@Test
fun `GithubViewModel이 생성되고, 통신이 실패하면, 에러메시지를 가져온다`() = runTest {
// given
val errorMessage = "통신에 실패하였습니다."
coEvery { networkRepository.getRepositories() } returns Result.failure(
Throwable(errorMessage)
)

// when
viewModel = GithubViewModel(networkRepository)

// then
coVerify { networkRepository.getRepositories() }
assertThat(viewModel.uiState.value).isEqualTo(
UiState.Error(errorMessage)
)
}

}
28 changes: 28 additions & 0 deletions app/src/test/java/camp/nextstep/edu/github/MainDispatcherRule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @author Daewon on 02,September,2023
*
*/

package camp.nextstep.edu.github

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

@OptIn(ExperimentalCoroutinesApi::class)
class MainDispatcherRule(
private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()
) : TestWatcher() {
override fun starting(description: Description) {
Dispatchers.setMain(testDispatcher)
}

override fun finished(description: Description) {
Dispatchers.resetMain()
}
}

0 comments on commit 7e7887b

Please sign in to comment.