-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
app/src/test/java/camp/nextstep/edu/github/ExampleUnitTest.kt
This file was deleted.
Oops, something went wrong.
94 changes: 94 additions & 0 deletions
94
app/src/test/java/camp/nextstep/edu/github/GithubViewModelTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
app/src/test/java/camp/nextstep/edu/github/MainDispatcherRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |