Sample example about demonstrating how to architect a multi-module project.
@ExperimentalCoroutinesApi
class CharacterDetailViewModelTest {
private val characterUiModel = mockk<CharacterUiModel>(relaxed = true)
private val error = mockk<Throwable>(relaxed = true)
@get:Rule
val testCoroutineRule = TestCoroutineRule()
@Test
fun `test character detail success state`() = testCoroutineRule.runTest {
// Given
val characterDetailUseCase = object : ICharacterDetailUseCase {
override suspend fun invoke(id: Int): Flow<Result<CharacterUiModel>> = flow {
emit(Result.success(characterUiModel))
}
}
val viewModel = CharacterDetailViewModel(characterDetailUseCase)
// When
characterDetailUseCase.invoke(1).collect { result ->
result.fold(
onSuccess = { uiModel ->
viewModel.updateUiState(uiModel)
},
onFailure = {}
)
// Then
viewModel.uiState.test {
assertEquals(result.getOrNull(), awaitItem().character)
}
}
}
@Test
fun `test character detail error state`() = testCoroutineRule.runTest {
// Given
var errorState = Throwable()
val characterDetailUseCase = object : ICharacterDetailUseCase {
override suspend fun invoke(id: Int): Flow<Result<CharacterUiModel>> = flow {
emit(Result.failure(error))
}
}
// When
characterDetailUseCase.invoke(1).collectLatest { result ->
result.fold(
onSuccess = { },
onFailure = { errorState = it }
)
// Then
assertEquals(result.exceptionOrNull(), errorState)
}
}
}
- Kotlin
- Coroutines
- Sandwich
- Flow
- Navigation
- Android Architecture Components
- Hilt
- Paging3.0
- Retrofit
- GSON
- Glide
- Turbine
- Material Components for Android
Copyright 2022 Doğan Can Kılıç
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.