Skip to content
Back to skills

Android Testing

ASecurity

Use `kotlinx-coroutines-test` to replace `Dispatchers.Main`, because `viewModelScope` launches on the main dispatcher. Inject the repository or use case and mock it with MockK; the test must not make a real network call or depend on a real database. ```kotlin @OptIn(ExperimentalCoroutinesApi::class) class MainDispatcherRule( val testDispatcher: TestDispatcher = UnconfinedTestDispatcher() ) : TestWatcher() { override fun starting(description: Description) { Dispatchers.setMain(testDispatcher) ...

  • 584 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 5, 2026
developmentkotlintestingapidatabase

Works with

  • api

Security analysis

A100/100

Pro scans all 18 files and shows the line behind each finding

Scanned September 5, 2026

npx -y skills add HoangNguyen0403/agent-skills-standard --skill android-testing --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Android Testing?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Android Testing
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/hoangnguyen0403-android-testing-agent-skills-standard/badge)](https://www.skillsdirectory.com/skills/hoangnguyen0403-android-testing-agent-skills-standard)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
# Unit-testing a `viewModelScope` `StateFlow`

Use `kotlinx-coroutines-test` to replace `Dispatchers.Main`, because `viewModelScope` launches on the main dispatcher. Inject the repository or use case and mock it with MockK; the test must not make a real network call or depend on a real database.

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

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

Example production code:

```kotlin
class UserViewModel(
    private val repository: UserRepository,
) : ViewModel() {
    private val _uiState = MutableStateFlow<UiState>(UiState.Loading)
    val uiState: StateFlow<UiState> = _uiState.asStateFlow()

    fun loadUser() {
        viewModelScope.launch {
            _uiState.value = try {
                UiState.Success(repository.loadUser())
            } catch (error: Throwable) {
                UiState.Error(error)
            }
        }
    }
}
```

Test the public action and the observable state rather than reaching into the ViewModel’s private job:

```kotlin
@OptIn(ExperimentalCoroutinesApi::class)
class UserViewModelTest {
    @get:Rule
    val mainDispatcherRule = MainDispatcherRule()

    private val repository = mockk<UserRepository>()

    @Test
    fun `loadUser emits success`() = runTest {
        val expected = User(id = "42", name = "Ada")
        coEvery { repository.loadUser() } returns expected
        val viewModel = UserViewModel(repository)

        assertEquals(UiState.Loading, viewModel.uiState.value)

        viewModel.loadUser()
        advanceUntilIdle()

        assertEquals(UiState.Success(expected), viewModel.uiState.value)
        coVerify(exactly = 1) { repository.loadUser() }
    }
}
```

`runTest` provides virtual coroutine execution; `advanceUntilIdle()` drains queued work without a wall-clock sleep. Use `runCurrent()` for work due at the current virtual time and `advanceTimeBy(...)` for deliberate delays or timeouts. Add separate tests for loading, repository failure, repeated calls, and cancellation. If every intermediate emission matters, collect the `StateFlow` with a coroutine-test collector such as Turbine and assert each state in order.


Files in this skill

  • eval-1.baseline.md2.7 KB
  • eval-1.with-skill.md2.4 KB
  • eval-2.baseline.md2.8 KB
  • eval-2.with-skill.md2.5 KB
  • eval-3.baseline.md2.2 KB
  • eval-3.with-skill.md1.8 KB
  • trigger-1.md137 B
  • trigger-10.md139 B
  • trigger-11.md143 B
  • trigger-2.md143 B
  • trigger-3.md139 B
  • trigger-4.md150 B
  • trigger-5.md143 B
  • trigger-6.md146 B
  • trigger-7.md127 B
  • trigger-8.md152 B
  • trigger-9.md142 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.