agentsclimarketplace

Testing strategy

Skill thetruong1099/android-mvi-base-code/.claude/skills/testing-strategy

Testing patterns for this project. Use when writing ViewModel unit tests with MainDispatcherRule and MockK, UseCase tests mocking repositories, Repository tests with data source mocks, or Compose UI tests with FakeBaseViewModel and TemplateThemePreview. Covers Google Truth assertions and test naming conventions.From its SKILL.md

Install
npx -y skills add thetruong1099/android-mvi-base-code --skill testing-strategy

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

2 things to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
  • 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.

SKILL.md

6.7 KB, ~1.4k tokens by cl100k_base, as published. Nobody here has run it

Testing Strategy

Convention Plugins

PluginUse forProvides
android.test.unitViewModel, UseCase, RepositoryJUnit + MockK + Coroutines Test + Truth
android.test.instrumentationCompose UI testsAndroidJUnit4 + Compose Test
android.test.robolectricAndroid unit testsRobolectric

Add to build.gradle.kts:

plugins {
    alias(libs.plugins.android.feature.compose)
    alias(libs.plugins.android.test.unit)  // Add for tests
}

Test File Locations

feature/<name>/src/test/           -> ViewModel unit tests
domain/usecase/src/test/           -> UseCase unit tests
data/remote-data/src/test/         -> Repository unit tests
feature/<name>/src/androidTest/    -> Compose UI tests

1. ViewModel Unit Test

@OptIn(ExperimentalCoroutinesApi::class)
class SampleViewModelTest {

    @get:Rule
    val mainDispatcherRule = MainDispatcherRule()

    private lateinit var viewModel: SampleViewModel
    private val getSampleUseCase: GetSampleUseCase = mockk()

    @Before
    fun setup() {
        every { getSampleUseCase() } returns flowOf(PagingData.empty())
        viewModel = SampleViewModel(getSampleUseCase)
    }

    @Test
    fun `initial state should have paging flow set`() {
        assertThat(viewModel.uiState.value.items).isNotNull()
    }

    @Test
    fun `OnItemClick should emit NavigateToDetail effect`() = runTest {
        val item = SampleModel(id = "123", name = "Test Item")

        viewModel.onTriggerEvent(SampleViewEvent.OnItemClick(item))

        assertThat(viewModel.effectState.value)
            .isInstanceOf(SampleViewEffect.NavigateToDetail::class.java)
        assertThat((viewModel.effectState.value as SampleViewEffect.NavigateToDetail).id)
            .isEqualTo("123")
    }
}

MainDispatcherRule

Located in the test source of the first feature module that uses it. Copy to each test module that needs it:

@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()
    }
}

2. UseCase Unit Test

class GetDetailUseCaseTest {

    private val repository: ItemRepository = mockk()
    private lateinit var useCase: GetDetailUseCaseImpl

    @Before
    fun setup() {
        useCase = GetDetailUseCaseImpl(repository)
    }

    @Test
    fun `invoke should return item from repository`() = runTest {
        val expectedItem = Item(id = "1", name = "Test")
        coEvery { repository.getItemDetail("1") } returns flowOf(
            DataState.Success(expectedItem)
        )

        useCase(GetDetailParam(id = "1")).collect { state ->
            when (state) {
                is DataState.Success -> assertThat(state.data).isEqualTo(expectedItem)
                else -> fail("Expected Success")
            }
        }
    }

    @Test
    fun `invoke should propagate repository errors`() = runTest {
        coEvery { repository.getItemDetail("1") } returns flowOf(
            DataState.Error(AppError.ServerError(500))
        )

        useCase(GetDetailParam(id = "1")).collect { state ->
            assertThat(state).isInstanceOf(DataState.Error::class.java)
        }
    }
}

3. Repository Unit Test

class ItemRepositoryImplTest {

    private val remoteDataSource: ItemRemoteDataSource = mockk()
    private val itemMapper: ItemMapper = mockk()
    private lateinit var repository: ItemRepositoryImpl

    @Before
    fun setup() {
        repository = ItemRepositoryImpl(remoteDataSource, itemMapper)
    }

    @Test
    fun `getItemDetail should map DTO to domain model`() = runTest {
        val dto = ItemDto(id = "1", name = "Test")
        val domain = Item(id = "1", name = "Test")
        coEvery { remoteDataSource.getItemById("1") } returns flowOf(DataState.Success(dto))
        every { itemMapper.toDomain(dto) } returns domain

        repository.getItemDetail("1").collect { state ->
            when (state) {
                is DataState.Success -> assertThat(state.data).isEqualTo(domain)
                else -> {}
            }
        }
    }
}

4. Compose UI Test

@HiltAndroidTest
class SampleScreenTest {

    @get:Rule
    val composeTestRule = createComposeRule()

    @Test
    fun `SampleScreenInternal displays content`() {
        val fakeViewModel = FakeBaseViewModel<SampleViewState, SampleViewEvent, SampleViewEffect>(
            initialState = SampleViewState()
        )

        composeTestRule.setContent {
            TemplateThemePreview { SampleScreenInternal(viewModel = fakeViewModel) }
        }

        composeTestRule.onNodeWithText("Sample").assertIsDisplayed()
    }

    @Test
    fun `loading state shows loading indicator`() {
        val fakeViewModel = FakeBaseViewModel<SampleViewState, SampleViewEvent, SampleViewEffect>(
            initialState = SampleViewState(),
            initialLoading = true
        )

        composeTestRule.setContent {
            TemplateThemePreview { SampleScreenInternal(viewModel = fakeViewModel) }
        }

        composeTestRule.onNode(hasTestTag("loading_indicator")).assertIsDisplayed()
    }
}

Test Naming Convention

Pattern: `[method/action] [condition] should [expected result]`

fun `onTriggerEvent OnItemClick should set NavigateToDetail effect`()
fun `collectDataState with server error should call onError`()
fun `initial state should have empty list and loading false`()

Test Priority

High (test first)

  1. ViewModels - onTriggerEvent(), state transitions, effect emissions
  2. UseCases with business logic
  3. ExceptionMapper - exception -> AppError mappings
  4. ErrorHandler - AppError -> string resource mappings

Medium

  1. Repository implementations - DTO -> Domain, flow logic
  2. BaseDataSource strategies - success/error flows

Lower

  1. Compose UI tests - screen rendering, user interactions
  2. Navigation tests

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.