Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
bitwarden avatar

Testing Android Code

  • 69 installs
  • 9.2k repo stars
  • Updated August 4, 2026
  • bitwarden/android

testing-android-code is a Claude Code skill that gives tactical testing patterns for Bitwarden Android ViewModels, Compose screens, repositories, and data sources.

About

This skill provides tactical testing guidance for Bitwarden-specific Android patterns. It documents base test classes, ViewModel and Compose UI testing, repository and service testing with MockWebServer, test data builders, Result-type testing, and common gotchas. A developer uses it when writing or reviewing tests for Android code in Bitwarden. It relies on JUnit 5, MockK, and Turbine.

  • Tactical testing patterns for Bitwarden Android ViewModels, Compose, repositories, and services
  • Documents base classes (BaseViewModelTest, BitwardenComposeTest, BaseServiceTest) and 35+ mock builders
  • Covers StateFlow vs EventFlow, MockK, Turbine, and coroutine test gotchas

Testing Android Code by the numbers

  • 69 all-time installs (skills.sh)
  • Ranked #1,099 of 2,153 Testing & QA skills by installs in the Skillselion catalog
  • Data as of Aug 5, 2026 (Skillselion catalog sync)
At a glance

testing-android-code capabilities & compatibility

Capabilities
unit testing · compose testing · repository testing · code review
Use cases
testing · debugging
From the docs

What testing-android-code says it does

This skill provides tactical testing guidance for Bitwarden-specific patterns.
SKILL.md
Always extend `BaseViewModelTest` for ViewModel tests.
SKILL.md
npx skills add https://github.com/bitwarden/android --skill testing-android-code

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs69
repo stars9.2k
Last updatedAugust 4, 2026
Repositorybitwarden/android

What it does

Write or fix Bitwarden Android tests using the project's base classes, mock builders, and StateFlow/EventFlow patterns.

Who is it for?

Writing ViewModel, Compose, repository, and service tests in the Bitwarden Android codebase.

Skip if: Non-Bitwarden test suites or non-Android testing.

When should I use this skill?

When writing or reviewing tests for ViewModels, repositories, Compose screens, or data sources, or debugging a failing Bitwarden test.

What you get

Correct, idiomatic Bitwarden Android tests for ViewModels, Compose, repositories, and services.

  • ViewModel test patterns
  • Compose UI test patterns
  • Repository/service test patterns

By the numbers

  • 35+ mock data builders
  • 7 pattern sections (A-G)
  • base classes: BaseViewModelTest, BitwardenComposeTest, BaseServiceTest

Files

SKILL.mdMarkdownGitHub ↗

Testing Android Code - Bitwarden Testing Patterns

This skill provides tactical testing guidance for Bitwarden-specific patterns. For comprehensive architecture and testing philosophy, consult docs/ARCHITECTURE.md.

Test Framework Configuration

Required Dependencies:

  • JUnit 5 (jupiter), MockK, Turbine (app.cash.turbine)
  • kotlinx.coroutines.test, Robolectric, Compose Test

Critical Note: Tests run with en-US locale for consistency. Don't assume other locales.

---

A. ViewModel Testing Patterns

Base Class: BaseViewModelTest

Always extend `BaseViewModelTest` for ViewModel tests.

Location: ui/src/testFixtures/kotlin/com/bitwarden/ui/platform/base/BaseViewModelTest.kt

Benefits:

  • Automatically registers MainDispatcherExtension for UnconfinedTestDispatcher
  • Provides stateEventFlow() helper for simultaneous StateFlow/EventFlow testing

Pattern:

class ExampleViewModelTest : BaseViewModelTest() {
    private val mockRepository: ExampleRepository = mockk()
    private val savedStateHandle = SavedStateHandle(mapOf(KEY_STATE to INITIAL_STATE))

    @Test
    fun `ButtonClick should fetch data and update state`() = runTest {
        coEvery { mockRepository.fetchData(any()) } returns Result.success("data")

        val viewModel = ExampleViewModel(savedStateHandle, mockRepository)

        viewModel.stateFlow.test {
            assertEquals(INITIAL_STATE, awaitItem())
            viewModel.trySendAction(ExampleAction.ButtonClick)
            assertEquals(INITIAL_STATE.copy(data = "data"), awaitItem())
        }

        coVerify { mockRepository.fetchData(any()) }
    }
}

For complete examples: See references/test-base-classes.md

StateFlow vs EventFlow (Critical Distinction)

Flow TypeReplayFirst ActionPattern
StateFlowYes (1)awaitItem() gets current stateExpect initial → trigger → expect new
EventFlowNoexpectNoEvents() firstexpectNoEvents → trigger → expect event

For detailed patterns: See references/flow-testing-patterns.md

---

B. Compose UI Testing Patterns

Base Class: BitwardenComposeTest

Always extend `BitwardenComposeTest` for Compose screen tests.

Location: app/src/test/kotlin/com/x8bit/bitwarden/ui/platform/base/BitwardenComposeTest.kt

Benefits:

  • Pre-configures all Bitwarden managers (FeatureFlags, AuthTab, Biometrics, etc.)
  • Wraps content in BitwardenTheme and LocalManagerProvider
  • Provides fixed Clock for deterministic time-based tests

Pattern:

class ExampleScreenTest : BitwardenComposeTest() {
    private var haveCalledNavigateBack = false
    private val mutableEventFlow = bufferedMutableSharedFlow<ExampleEvent>()
    private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
    private val viewModel = mockk<ExampleViewModel>(relaxed = true) {
        every { eventFlow } returns mutableEventFlow
        every { stateFlow } returns mutableStateFlow
    }

    @Before
    fun setup() {
        setContent {
            ExampleScreen(
                onNavigateBack = { haveCalledNavigateBack = true },
                viewModel = viewModel,
            )
        }
    }

    @Test
    fun `on back click should send BackClick action`() {
        composeTestRule.onNodeWithContentDescription("Back").performClick()
        verify { viewModel.trySendAction(ExampleAction.BackClick) }
    }
}

Note: Use bufferedMutableSharedFlow for event testing in Compose tests. Default replay is 0; pass replay = 1 if needed.

For complete base class details: See references/test-base-classes.md

---

C. Repository and Service Testing

Service Testing with MockWebServer

Base Class: BaseServiceTest (network/src/testFixtures/)

class ExampleServiceTest : BaseServiceTest() {
    private val api: ExampleApi = retrofit.create()
    private val service = ExampleServiceImpl(api)

    @Test
    fun `getConfig should return success when API succeeds`() = runTest {
        server.enqueue(MockResponse().setBody(EXPECTED_JSON))
        val result = service.getConfig()
        assertEquals(EXPECTED_RESPONSE.asSuccess(), result)
    }
}

Repository Testing Pattern

class ExampleRepositoryTest {
    private val fixedClock: Clock = Clock.fixed(
        Instant.parse("2023-10-27T12:00:00Z"),
        ZoneOffset.UTC,
    )
    private val dispatcherManager = FakeDispatcherManager()
    private val mockDiskSource: ExampleDiskSource = mockk()
    private val mockService: ExampleService = mockk()

    private val repository = ExampleRepositoryImpl(
        clock = fixedClock,
        exampleDiskSource = mockDiskSource,
        exampleService = mockService,
        dispatcherManager = dispatcherManager,
    )

    @Test
    fun `fetchData should return success when service succeeds`() = runTest {
        coEvery { mockService.getData(any()) } returns expectedData.asSuccess()
        val result = repository.fetchData(userId)
        assertTrue(result.isSuccess)
    }
}

Key patterns: Use FakeDispatcherManager, fixed Clock, and .asSuccess() helpers.

---

D. Test Data Builders

Builder Pattern with Number Parameter

Location: network/src/testFixtures/kotlin/com/bitwarden/network/model/

fun createMockCipher(
    number: Int,
    id: String = "mockId-$number",
    name: String? = "mockName-$number",
    // ... more parameters with defaults
): SyncResponseJson.Cipher

// Usage:
val cipher1 = createMockCipher(number = 1)  // mockId-1, mockName-1
val cipher2 = createMockCipher(number = 2)  // mockId-2, mockName-2
val custom = createMockCipher(number = 3, name = "Custom")

Available Builders (35+):

  • Cipher: createMockCipher(), createMockLogin(), createMockCard(), createMockIdentity(), createMockSecureNote(), createMockSshKey(), createMockField(), createMockUri(), createMockFido2Credential(), createMockPasswordHistory(), createMockCipherPermissions()
  • Sync: createMockSyncResponse(), createMockFolder(), createMockCollection(), createMockPolicy(), createMockDomains()
  • Send: createMockSend(), createMockFile(), createMockText(), createMockSendJsonRequest()
  • Profile: createMockProfile(), createMockOrganization(), createMockProvider(), createMockPermissions()
  • Attachments: createMockAttachment(), createMockAttachmentJsonRequest(), createMockAttachmentResponse()

See network/src/testFixtures/kotlin/com/bitwarden/network/model/ for full list.

---

E. Result Type Testing

Locations:

  • .asSuccess(), .asFailure(): core/src/main/kotlin/com/bitwarden/core/data/util/ResultExtensions.kt
  • assertCoroutineThrows: core/src/testFixtures/kotlin/com/bitwarden/core/data/util/TestHelpers.kt
// Create results
"data".asSuccess()              // Result.success("data")
throwable.asFailure()           // Result.failure<T>(throwable)

// Assertions
assertTrue(result.isSuccess)
assertEquals(expectedValue, result.getOrNull())

---

F. Test Utilities and Helpers

Fake Implementations

FakeLocationPurpose
FakeDispatcherManagercore/src/testFixtures/Deterministic coroutine execution
FakeConfigDiskSourcedata/src/testFixtures/In-memory config storage
FakeSharedPreferencesdata/src/testFixtures/Memory-backed SharedPreferences

Exception Testing (CRITICAL)

// CORRECT - Call directly, NOT inside runTest
@Test
fun `test exception`() {
    assertCoroutineThrows<IllegalStateException> {
        repository.throwingFunction()
    }
}

Why: runTest catches exceptions and rethrows them, breaking the assertion pattern.

---

G. Critical Gotchas

Common testing mistakes in Bitwarden. For complete details and examples: See references/critical-gotchas.md

Core Patterns:

  • assertCoroutineThrows + runTest - Never wrap in runTest; call directly
  • Static mock cleanup - Always unmockkStatic() in @After
  • StateFlow vs EventFlow - StateFlow: awaitItem() first; EventFlow: expectNoEvents() first
  • FakeDispatcherManager - Always use instead of real DispatcherManagerImpl
  • Coroutine test wrapper - Use runTest { } for all Flow/coroutine tests

Assertion Patterns:

  • Complete state assertions - Assert entire state objects, not individual fields
  • JUnit over Kotlin - Use assertTrue(), not Kotlin's assert()
  • Use Result extensions - Use asSuccess() and asFailure() for Result type assertions

Test Design:

  • Fake vs Mock strategy - Use Fakes for happy paths, Mocks for error paths
  • DI over static mocking - Extract interfaces (like UuidManager) instead of mockkStatic
  • Null stream testing - Test null returns from ContentResolver operations
  • bufferedMutableSharedFlow - Use with .onSubscription { emit(state) } in Fakes
  • Test factory methods - Accept domain state types, not SavedStateHandle

---

H. Test File Organization

Directory Structure

module/src/test/kotlin/com/bitwarden/.../
├── ui/*ScreenTest.kt, *ViewModelTest.kt
├── data/repository/*RepositoryTest.kt
└── network/service/*ServiceTest.kt

module/src/testFixtures/kotlin/com/bitwarden/.../
├── util/TestHelpers.kt
├── base/Base*Test.kt
└── model/*Util.kt

Test Constants Placement

Declare test constants as top-level private const val at the bottom of the file, after the class closing brace. Do NOT use companion object for test constants.

Test Naming

  • Classes: *Test.kt, *ScreenTest.kt, *ViewModelTest.kt
  • Functions: ` given state when action should result `

---

Summary

Key Bitwarden-specific testing patterns:

1. BaseViewModelTest - Automatic dispatcher setup with stateEventFlow() helper 2. BitwardenComposeTest - Pre-configured with all managers and theme 3. BaseServiceTest - MockWebServer setup for network testing 4. Turbine Flow Testing - StateFlow (replay) vs EventFlow (no replay) 5. Test Data Builders - Consistent number: Int parameter pattern 6. Fake Implementations - FakeDispatcherManager, FakeConfigDiskSource 7. Result Type Testing - .asSuccess(), .asFailure()

Always consult: docs/ARCHITECTURE.md and existing test files for reference implementations.

---

Reference Documentation

For detailed information, see:

  • references/test-base-classes.md - Detailed base class documentation and usage patterns
  • references/flow-testing-patterns.md - Complete Turbine patterns for StateFlow/EventFlow
  • references/critical-gotchas.md - Full anti-pattern reference and debugging tips

Complete Examples:

  • examples/viewmodel-test-example.md - Full ViewModel test with StateFlow/EventFlow
  • examples/compose-screen-test-example.md - Full Compose screen test
  • examples/repository-test-example.md - Full repository test with mocks and fakes

Related skills

Testing & QAtestingbackend

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.